Sorting List and Array in Java

May 11, 2020 | No comments

Java Sorting with List and Array

Array and List are some of the basic and very useful data structures. We often need to sort the data stored in Array and List. In this article, we examine some of the most common ways of sorting. Java provides Collections.sort class to sort Collections like List, Set, etc. Similarly, java also provides Arrays.sort() to sort Arrays. Both Collections.sort() and Arrays.sort() also takes Comparable instance which can be used to sort the data.

List Sorting

The following example shows different ways of sorting the Java List. We will be using Collections.sort() to sort the list. Collections class is part of the java.util package, and the sort method accepts a Comparator also. If we pass Comparator then the Comparator will be used to sort the objects in the collection. If Comparator is not passed then the natural ordering of the objects will be used to sort. Comparator.sort() takes a list of any objects provided that object implements a Comparable interface.
Create List and Add Data
Following code creates a list and adds data to it.
List nameList = new ArrayList();
nameList.add("Brazil");
nameList.add("Canada");
nameList.add("Japan");
nameList.add("China");
nameList.add("Australia");
nameList.add("Russia");
nameList.add("Nepal");
nameList.add("Italy");
nameList.add("Germany");
Collections.sort
The following code will sort the list using the natural ordering of the items in the list. For the above array, it will print Australia, Brazil, Canada, China, Germany, Italy, Japan, Nepal, Russia,
printMessage("Sort List ......");
Collections.sort(nameList);
nameList.forEach(a -> nicePrint(a));
Collection.sort with reverseOrder()
Like above now we are sorting using Collections.sort, but also passing reverseOrder() comparator. It will print Russia, Nepal, Japan, Italy, Germany, China, Canada, Brazil, Australia,
printMessage("Sort List Reverse Order .......");
Collections.sort(nameList, Collections.reverseOrder());
nameList.forEach(a -> nicePrint(a));
Collection.sort using a custom Comparator
Following code, sorts using Collections.sort and passed custom Comparator implemented using Lambda. It will print Australia, Brazil, Canada, China, Germany, Italy, Japan, Nepal, Russia,
printMessage("Sort List using custom Comparator");
Collections.sort(nameList, (k1, k2) -> (k1.compareTo(k2)));
nameList.forEach(a -> nicePrint(a));
Sort Part of the List
We can also sort part of the list. For that, we can pass subList() of the actual list to the Collections.sort() method. The following code will print Brazil, Canada, Japan, China, Australia, Russia, Nepal, Italy, Germany. See the first 3 country names are sorted.
Collections.sort(nameList.subList(0, 3));
nameList.forEach(a -> nicePrint(a));

Array Sorting

The following example shows different ways of sorting the Java Array. We will be using Arrays.sort() to sort the array.
Sort Array
Sort the data array using Arrays.sort(). It will print the following It will print Australia, Brazil, Canada, China, Germany, Italy, Japan, Nepal, Russia.
Arrays.sort(data);
Sort Array in reverse order
Sort in reverse order, bypassing reverseOrder() to the Arrays.sort() method. It will print Russia, Nepal, Japan, Italy, Germany, China, Canada, Brazil, Australia,
Arrays.sort(data, Collections.reverseOrder());
Sort Array by using a custom comparator
Following code sorts data using Arrays.sort and a custom Comparator by using lambda.
Arrays.sort(data, (k1, k2) -> (k1.compareTo(k2)));
Sorting part of Array
We can sort part of an array also, the following example shows how to sort only the first 3 items in the Array.
Arrays.sort(data, 0, 3);
Sorting Array using parallel sort.
We can also sort the data by using parallelSort().
Arrays.parallelSort(data);
Summary
  • Collections.sort() can be used to sort a list of objects.
  • Collections.sort() uses BinarySearch algorithm.
  • Arrays.sort() uses Mergesort or Timsort algorithms to sort the data.
  • Collections.sort() and Arrays.sort() throws NullPointerException if data is null.
  • The object that we are sorting should implement java.langComparable interface.

No comments :

Post a Comment

Please leave your message queries or suggetions.