Hit enter after type your search item

Ways to perform Java sorting by built-in methods

Java has a built-in method for sorting the elements in collections like ArrayLists, LinkedList etc. The arrays class also has a built-in method “sort” that enables sorting the arrays only.

You may sort the elements of arrays or collections in ascending or descending order. In this tutorial, I will show you how to use the Java array sort method along with java.util.collections.sort method for ArrayList and other collections and bubbleSort algorithm(in the last section), so keep reading.

A few examples of sorting

Java array sort method

First, let me start with the array sort method. You may skip to next section for learning about the Collections sort method.

The method for sorting arrays belongs to java.util.Arrays.sort(). The sort method of arrays does not return any value but simply sorts the given array in ascending order. You may use the sort method of arrays for sorting the numeric or string arrays.

Syntax of using the sort method in Arrays

Following is the general syntax of using the sort method of arrays class:

public static void sort(array_to_sort)

In this way, the given array elements will be sorted into ascending numerical order. The method returns no value.

You may also specify the selection of elements to sort in the given array as follows:


In that way:
  • The array argument is the array you want to sort. This can be a primitive type like int, float, byte etc. or objects.
  • The fromIndex parameter specifies the starting index position (inclusive).
  • While the toindex specifies the end index in the given array (non-inclusive).
  • The elements will be sorted in ascending order for the given range.
Note: For sorting the elements in descending order, see the example number 2 and 3 below.

An example of sorting an int array

For sorting arrays demo, an array of int elements is created with random order. After that, the sort Java method is used for sorting the elements in ascending order. For seeing the difference, the array elements before and after using the sort method is displayed:

See online demo and code


sort int array Java

Demo of sorting elements in descending order

For sorting the arrays in descending order you may use the arrays.sort() method with the Collections.reverseOrder() method. However, the reverseOrder() method works with objects only. So, if you have a primitive type array like in above example, it will not work.

For this example, an array of Integer type is created that contains random numbers. After that, sort method with reverseOrder() is used for sorting in descending order:

See online demo and code


sort int array DESC

Sort a string array example

Similarly, you may sort a string array that contains string elements in random order. For sorting a string array example, I created an array with alphabets in random order.

The Java array sort method is used twice:

  • First for sorting string elements in ascending order
  • The second uses Collections.reverseOrder() method with sort() for descending order

Have a look:

See online demo and code


Java array sort

Note: Do not forget to import java.util.Collections in order to sort in descending order for arrays.

Sorting a selection of array elements example

As mentioned earlier, the array sort method also allows sorting the given range of array elements. For example, you only need to sort the elements from 3 to 7 index positions.


The output of above code is:

Original int array: [15, 13, 17, 11, 19, 21, 3, 7, 5]

Array range sorting: [15, 13, 3, 11, 17, 19, 21, 7, 5]

You can see, the elements 0 and 1 remained in the same place even after using the sort() method. The sorting started at index 2 and ended at 6 position. The reason is endindex parameter does not include the given number.

A simple solution for primitive type descending order

As mentioned earlier, the primitive type array cannot be sorted in descending order by using the Collections.reverseOrder() method. You may develop your own algorithm or use some other class by the third party if this is really required.

Following is a simple solution that you may use for primitive type array in descending order.


The output of this program:

Array before sorting: [7, 3, 13, 15, 5, 21, -1]

 

Array after sorting: [-1, 3, 5, 7, 13, 15, 21]

 

In descending order:

21

15

13

7

5

3

-1

You see, the int array is displayed in original order, in ascending order by using sort() method and finally in descending order.

Java Collections sort method

Now, let us explore the Collections sort method that enables list sort like ArrayList, LinkedList etc. The array sort method allows sorting only the elements of the array. However, you may use the Collections sort method for sorting different collections.

Syntax of using the Collections sort method

Following is the syntax for using the Collections.sort method:

sort(List<T> list)

Where:

  • List argument can be ArrayList, LinkedList etc.
  • The list sorting is in ascending order.
  • This method does not return any value.
  • The collection sort method can only be used for sorting objects, unlike the arrays sort where primitive type can also be sorted.
  • You may use the reverseOrder() method for ordering a list in descending order.

You may also use list sort method as follows:

sort(List<T> list, Comparator<? super T> c)

In this way, the list is sorted based on the specified comparator.

An example of sorting ArrayList items

For demonstrating the sort method of Collections, an ArrayList of 5 elements that are vowel letters is created in random order. After that, the sort method is used for ordering it in ascending order as follows:

See online demo and code


sort ArrayList Java

You can see the difference before and after using the sort method.

Example of Integer Array List sorting

For this example, the ArrayList of Integer objects is created in random order. The List is displayed before and after using the sort Collections method:

See online demo and code


sort ArrayList integer

Sorting string ArrayList in descending order example

The sorting of the list is done in ascending order as using the sort method. If you require getting the list in descending order then you may use the reverseOrder() method of Collections.

The following example shows a string list sorted in descending order:


The output of above example is:

Original List [a, u, o, e, i]

 

Sorted list in Ascending Order [a, e, i, o, u]

 

Sorted list in Descending Order [u, o, i, e, a]

Did you notice, this is the other way of using the Java Collections.sort method described in the syntax section? That is:

Collections.sort(List<T> list, Comparator<? super T> c)

Using  Collections reverse method for descending order

You may also use the reverse method of Collections for sorting the list in descending order. This is how you may use it:

Collections.reverse(list);

See an example below for making things clearer:


The output:

Original List [20, 5, 10, 25, 15]

 

Sorted list in Ascending Order [5, 10, 15, 20, 25]

 

Sorted list in Descending Order [25, 20, 15, 10, 5]

Is not that even cool?

Using bubble sort Algorithm for sorting arrays

You may also use the Bubble sort algo for sorting the arrays. The bubble sort algorithm swaps the elements one by one and if this element is greater than the next then it is swapped. The process goes on until all elements in the given array are entertained.

See the following example where you can see Bubble sort algorithm code along with an example array that is passed for sorting.


The output:

Array before Bubble sorting: [47, 13, 9, 19, 5, 1, -1, -11]

 

Array after Bubble sorting: [-11, -1, 1, 5, 9, 13, 19, 47]

The array is sorted in ascending order.

Note: The above solution applies to the primitive int and Integer based arrays.

Demo of sorting double array by Java Bubble sort algorithm

The above algorithm requires a little tweak for enabling double array sorting. See the example below where an array with double elements is created and passed to bubbleSort algo. Look carefully in the first part to see the difference between two algorithms:


The Output:

double array before Bubble sorting: [1.5, 9.5, 19.5, 5.0, 1.8, -1.4, -11.5]

 

double array after Bubble sorting: [-11.5, -1.4, 1.5, 1.8, 5.0, 9.5, 19.5]

Likewise, you may use the Double and Integer etc/ objects sorting as using the bubble sort solution.

Summarizing the sorting in Java

Java has a few methods for sorting the elements of arrays and collections in ascending and descending order.

  • The Java arrays sort method is for sorting array elements in ascending order. This method can be used for a primitive type like int, float etc as well as object arrays.
  • Similarly, the Collections has a built-in method “sort()” that is used for sorting the Lists. For example, ArrayList, LinkedList etc. The Collections.sort() method also sorts elements in ascending order.
  • For descending order of arrays and collections elements, you may use the reverse() and reverseOrder() methods of Collections.
This div height required for enabling the sticky sidebar