[Solved] How to sort list of objects? [duplicate]


You can use interface java.lang.Comparable in java

Example (pseudo code) assuming object.distance(player) returns an Integer value

class Distance implements Comparable<Distance>{
  /**
    * Compare a given Distance with this object.
    */
    public int compareTo(Distance o) {
        return this.distance(player).compareTo(o.distance(o.player));
    }
}

now you can sort your list like

Collections.sort(YourListOfDistance)

here some reference

When should a class be Comparable and/or Comparator?

solved How to sort list of objects? [duplicate]