Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Java Collection

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?

like image 847
Makky Avatar asked Aug 05 '11 14:08

Makky


People also ask

Can you sort a collection Java?

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.

How do I sort a collection list?

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.

Which sorting is used in Collections sort?

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.


2 Answers

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 CustomObjectimplements Comparable, then just use Collections.sort(list)

With JDK 8 the syntax is much simpler.

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.

like image 66
Kowser Avatar answered Oct 05 '22 18:10

Kowser


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); 
like image 38
Aliaksei Yatsau Avatar answered Oct 05 '22 18:10

Aliaksei Yatsau