I have a Java collection:
Collection<CustomObject> list = new ArrayList<CustomObject>();
CustomObject
has an id
field now before display list I want to sort this collection by that id
.
Is there any way I could that do that?
sort() method is present in java. util. Collections class. It is used to sort the elements present in the specified list of Collection in ascending order.
sort() to sort all List implementations such as LinkedList and ArrayList. There are two overloaded Collections. sort() methods, which are: sort(List list) : Sorts the elements of the List in ascending order of their natural ordering.
According to the Javadoc, only primitive arrays are sorted using Quicksort. Object arrays are sorted with a Mergesort as well. So Collections. sort seems to use the same sorting algorithm as Arrays.
Use a Comparator:
List<CustomObject> list = new ArrayList<CustomObject>(); Comparator<CustomObject> comparator = new Comparator<CustomObject>() { @Override public int compare(CustomObject left, CustomObject right) { return left.getId() - right.getId(); // use your logic } }; Collections.sort(list, comparator); // use the comparator as much as u want System.out.println(list);
Additionally, if CustomObject
implements Comparable
, then just use Collections.sort(list)
List<CustomObject> list = getCustomObjectList(); Collections.sort(list, (left, right) -> left.getId() - right.getId()); System.out.println(list);
Much simplier
List<CustomObject> list = getCustomObjectList(); list.sort((left, right) -> left.getId() - right.getId()); System.out.println(list);
Simplest
List<CustomObject> list = getCustomObjectList(); list.sort(Comparator.comparing(CustomObject::getId)); System.out.println(list);
Obviously the initial code can be used for JDK 8 too.
The question is: "Sort Collection". So you can't use Collections.sort(List<T> l, Comparator<? super T> comparator)
.
Some tips:
For Collection type:
Comparator<String> defaultComparator = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }; Collection<String> collection = getSomeStringCollection(); String[] strings = collection.toArray(new String[collection.size()]); Arrays.sort(strings, defaultComparator); List<String> sortedStrings = Arrays.asList(strings); Collection<String> collection = getSomeStringCollection(); List<String> list = new ArrayList(collection); Collections.sort(list, defaultComparator); collection = list; // if you wish
For List type:
List<String> list = getSomeStringList(); Collections.sort(list, defaultComparator);
For Set type:
Set<String> set = getSomeStringSet(); // Than steps like in 'For Collection type' section or use java.util.TreeSet // TreeSet sample: // Sorted using java.lang.Comparable. Set<String> naturalSorted = new TreeSet(set); Set<String> set = getSomeStringSet(); Set<String> sortedSet = new TreeSet(defaultComparator); sortedSet.addAll(set);
Java 8 version. There is java.util.List#sort(Comparator<? super E> c)
method
List<String> list = getSomeStringList(); list.sort(defaultComparator);
or
List<String> list = getSomeStringList(); list.sort((String o1, String o2) -> o1.compareTo(o2));
or for types that implements Comparable:
List<String> list = getSomeStringList(); list.sort(String::compareTo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With