How to sort arraylist java objects?

So, I want the java to have an arraylist object.

I have object1.number and object2.number , object3.number , etc., but these objects have other properties besides number , such as name , distance , etc ....

So, if he would sort the string into array , it would be simple, put the string in temporal and let the other string take its place ... but in the araryList object, how can I do it?

Is it possible to simply move objects to this array position?

Thanks.

+7
source share
6 answers

Add your own comparator:

 Arrays.sort(yourArray, new Comparator<YourClass>() { @Override public int compare(YourClass o1, YourClass o2) { //compare object properties } }); 
+8
source

You need to implement a comparable interface.

implements Comparable

method doing the work

 public int compareTo(Object obj) { } 

Note that an object is often replaced with a full type due to the general syntax that can be used in the implementation statement (shown below).

Full example here in the training document , hope this helps

Full example (taking from the link above looks like this), I added this just in case the link is dead at some point

 import java.util.*; public class Name implements Comparable<Name> { private final String firstName, lastName; public Name(String firstName, String lastName) { if (firstName == null || lastName == null) throw new NullPointerException(); this.firstName = firstName; this.lastName = lastName; } public String firstName() { return firstName; } public String lastName() { return lastName; } public boolean equals(Object o) { if (o == null || !(o instanceof Name)) return false; Name n = (Name) o; return n.firstName.equals(firstName) && n.lastName.equals(lastName); } public int hashCode() { return 31*firstName.hashCode() + lastName.hashCode(); } public String toString() { return firstName + " " + lastName; } public int compareTo(Name n) { int lastCmp = lastName.compareTo(n.lastName); return (lastCmp != 0 ? lastCmp : firstName.compareTo(n.firstName)); } } 

Client code from the article:

 import java.util.*; public class NameSort { public static void main(String[] args) { Name nameArray[] = { new Name("John", "Smith"), new Name("Karl", "Ng"), new Name("Jeff", "Smith"), new Name("Tom", "Rich") }; List<Name> names = Arrays.asList(nameArray); Collections.sort(names); System.out.println(names); } } 
+4
source

For this purpose you need to use a comparator.

0
source

Based on your question, I suggest that you must implement the sorting algorithm yourself. If so, you can manipulate the position of the elements in an ArrayList, it is just slightly different from a regular array. Look at add(int index, E element) . The index parameter allows you to decide where to add an element to an ArrayList.

0
source

To manipulate ArrayList elements like an array of a traditional array, use ArrayList.set (int, E).

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#set%28int,%20E%29

0
source

Use for Collections.sort() to sort an ArrayList in Java 8:

 Collections.sort(array, new Comparator<Class>() { @Override public int compare(Class o1, Class o2) { //compare object properties } }); 
0
source

All Articles