ArrayList, Set Sorting in Ascending –
Descending Order Java
Sorting
List, Set, and ArrayList in Java in ascending and descending order is very easy,
You just need to know the correct API method to do that. For example Collections.sort() method will sort the collection passed to it,
doesn't return anything just sort the collection itself. From Java 8 onwards you can also use List.sort() method to sort a List in ascending or descending order directly without using Collections.sort() method. If you like to use Stream API, it also provide a sort() method to sort elements inside Stream. You can also use that to sort any List. For example, you can first convert a List to Stream and then sort the Stream and collect the result into another List.
If List is big then it make sense to use List.sort() method but if the List is small, you can use any method to sort the List in Java. It's also worth noting that the sort() method of Collections class in Java is overloaded where
another version takes a Comparator and sorts all the elements of Collection on
order defined by Comparator.
If we don't pass any Comparator then objects will be sorted based upon their natural order
like String will be sorted alphabetically or lexicographically. Integer will be
sorted numerically etc.
The default sorting order for an object is ascending order
like Integer will be sorted from low to
high while descending order is just opposite. Collections.reverseOrder() returns a Comparator which will be
used for sorting Objects in descending order.
Sorting List in ascending and descending order - Example
Here full
code example of how to sort List in Java in both ascending and descending order in Java. It's not difficult just to remember that Collections.sort() is a key
method that sorts objects of Collection. You can pass it Comparator for
defining sorting order. If no comparator passed then Object must implement a Comparable interface in order to be sorted.
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Java program Example of sorting List, ArrayList and Set on ascending and descending order
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Java program Example of sorting List, ArrayList and Set on ascending and descending order
* in Java.
* Collections.sort() method is used for sorting collection and Collections.reverseOrder()
* will sort elements of Collections in descending order.
* @author Javin Paul
*/
public class CollectionSortingExample{
public static void main(String args[]) throws InterruptedException{
// Sort List and Set in ascending order
// Sorting List in Java in ascending order in Java
List<Integer> list = Arrays.asList( 1, 2, 5, 9, 3, 6, 4, 7, 8);
System.out.println("Unsorted List in Java: " + list);
// Sorting List into Java now, Collections.sort() method will sort
* Collections.sort() method is used for sorting collection and Collections.reverseOrder()
* will sort elements of Collections in descending order.
* @author Javin Paul
*/
public class CollectionSortingExample{
public static void main(String args[]) throws InterruptedException{
// Sort List and Set in ascending order
// Sorting List in Java in ascending order in Java
List<Integer> list = Arrays.asList( 1, 2, 5, 9, 3, 6, 4, 7, 8);
System.out.println("Unsorted List in Java: " + list);
// Sorting List into Java now, Collections.sort() method will sort
// the collection passed
// to it. Doesn't return anything it just sorts the collection itself
Collections.sort(list); //sorting collection
System.out.println("List in Java sorted in ascending order: " + list);
// sorting List in descending order in Java, Collections.sort() method can be used
// to sort all element in descending order if we pass it comparator which can
// return descending order for elements. here is an example of sorting List
// in descending order in Java
// Collection class provides a in built-in comparator for that which can
// sort objects in reverse order i..e descending order for Integers or
// any other Object in Java
Collections.sort(list, Collections.reverseOrder());
System.out.println("Java List sorted in descending order: " + list);
// Any List implementation in Java like ArrayList, LinkedList
// can be sorted in ascending and descending order in Java by following above
// example, let's see a quick example for sorting ArrayList and LinkedList
// in ascending and descending order in Java
//Sorting ArrayList in ascending order in Java
ArrayList alphabets = new ArrayList();
alphabets.add("c");
alphabets.add("b");
alphabets.add("a");
alphabets.add("d");
System.out.println("Unsorted ArrayList in Java : " + alphabets);
//Sorting ArrayList into ascending order
Collections.sort(alphabets);
System.out.println("Sorted ArrayList in ascending order in Java : " + alphabets);
//Sorting ArrayList into descending order or reverse order in Java
Collections.sort(alphabets, Collections.reverseOrder());
System.out.println("ArrayList sort in descending order in Java : " + alphabets);
}
}
Output:
Unsorted List in Java: [1, 2, 5, 9, 3, 6, 4, 7, 8]
List in Java sorted in ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Java List sorted in descending order: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Unsorted ArrayList in Java : [c, b, a, d]
Sorted ArrayList in ascending order in Java : [a, b, c, d]
ArrayList sort in descending order in Java : [d, c, b, a]
// to it. Doesn't return anything it just sorts the collection itself
Collections.sort(list); //sorting collection
System.out.println("List in Java sorted in ascending order: " + list);
// sorting List in descending order in Java, Collections.sort() method can be used
// to sort all element in descending order if we pass it comparator which can
// return descending order for elements. here is an example of sorting List
// in descending order in Java
// Collection class provides a in built-in comparator for that which can
// sort objects in reverse order i..e descending order for Integers or
// any other Object in Java
Collections.sort(list, Collections.reverseOrder());
System.out.println("Java List sorted in descending order: " + list);
// Any List implementation in Java like ArrayList, LinkedList
// can be sorted in ascending and descending order in Java by following above
// example, let's see a quick example for sorting ArrayList and LinkedList
// in ascending and descending order in Java
//Sorting ArrayList in ascending order in Java
ArrayList alphabets = new ArrayList();
alphabets.add("c");
alphabets.add("b");
alphabets.add("a");
alphabets.add("d");
System.out.println("Unsorted ArrayList in Java : " + alphabets);
//Sorting ArrayList into ascending order
Collections.sort(alphabets);
System.out.println("Sorted ArrayList in ascending order in Java : " + alphabets);
//Sorting ArrayList into descending order or reverse order in Java
Collections.sort(alphabets, Collections.reverseOrder());
System.out.println("ArrayList sort in descending order in Java : " + alphabets);
}
}
Output:
Unsorted List in Java: [1, 2, 5, 9, 3, 6, 4, 7, 8]
List in Java sorted in ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Java List sorted in descending order: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Unsorted ArrayList in Java : [c, b, a, d]
Sorted ArrayList in ascending order in Java : [a, b, c, d]
ArrayList sort in descending order in Java : [d, c, b, a]
As shown in this sort example of ArrayList and Set, You can sort LinkedList in ascending and descending order in Java. Since the Collections.sort() method is applicable to the Collection
interface, any Collection implementation like Set, HashSet can also be sorted
in ascending, descending, or reverse order of elements.
Further Learning
What is the difference between ArrayList and Vector in Java?
How to iterate over ArrayList in Java
How to loop Map in Java with Example
Difference between ConcurrentHashMap and Hashtable in Java
Difference between Hashtable and HashMap in Java
Further Learning
What is the difference between ArrayList and Vector in Java?
How to iterate over ArrayList in Java
How to loop Map in Java with Example
Difference between ConcurrentHashMap and Hashtable in Java
Difference between Hashtable and HashMap in Java
By the way, Sorting arrays in Java is completely different than sorting collection in Java which we will see in the next tutorial. Now you learned how to sort Collection in Java including frequently used collections like ArrayList, List, Set, and HashSet.
Hi, I don't see any set there for sorting.
ReplyDeleteYes u r right No changes done..
DeleteHi, can you elaborate?
Delete