Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Sort Array List in Java

This article will help learning how to sort array list in java and arrange them in ascending as well as in descending order.

There may be a situation in front of a developer when he is required to arrange the array in Java. In that case we can utilize java.util.Arrays class that is capable of providing lots of utility method so as to sort java array of any type.

These types could include primitive, object or int, String etc. As far as arrays are concerned, it is in java.util package that is capable of exposing all sorting related method as static utility functions.

One can easily go ahead and access sort () as Arrays.sort() and just pass the array you are using and it will sort that array object. Array can be sorted in any order that can be ascending order, descending order or any custom order that are defined by custom comparator in Java. With the help of this article, we will take a glance at some of the examples of sorting array in ascending order, descending order and sorting sub arrays in java.

Sorting Array in Java in ascending and descending order

How to sort Java Array into Ascending Order?

There is not anything special that needs to be done so as to arrange or sort an array (String, int or any type) in ascending order.

  • Talking about the Arrays.sort() method, the elements are sorted by default and that too in their natural order that is implemented by CompareTo method.
  • And we will have string array in Java that will be sorted lexicographically.
  • Integer will be sorted numerically in an ascending order.

Let us take a close look on code section for complete example of how we can sort java array into an ascending order. The other method could be to sort anonymous array in Java with the help of this method however for the reason that we don’t have any name to refer that, it won’t make any meaning to do the same.

How to sort Java Array into Descending Order?

There is a need to provide an external Comparator in Java that would help in sorting a java array into descending order. This can help the sorting of elements in the reverse order and the good thing is that we have that built in as java.util.Collections.reverseOrder(). This will help in returning a reverse order comparator that is capable of providing reverse of natural ordering of element objects. You could pass this reverse comparator so as to sort method and it will help in sorting array into descending order. This is illustrated in the code example present on the last section of this article. One of the other ways that could be used to sort the array in descending order is to convert Array into ArrayList and then sort the arraylist. However it is again a two way step process thereby making it better to sort array using Arrays.sort() method.

How to Sort Sub Array in Java?

Arrays class also have the capability so as to provide methods so as to sort part of java array or sub array. In case you come across some of the big arrays however there is only a specific section that needs to be sorted, in that case, one can provide start index and end index to java.util.Arrays.sort(array,index,index). What will happen is that, it will then sort the array only for that range which is far better and faster than sorting whole array and then getting a range of values out of it.

The below section is used to display the code representation of sorting part of array in Java:

Brief on the Coding for Sorting Array in Java

The below code example illustrates the way to sort Java array into ascending order as well as descending order and also sorting sub arrays in Java:

Listing 1: Representing the code to sort array in ascending and descending order

Import java.util.Arrays;
import java.util.Collections;

public class HashtableDemo {

public static void main(String args[]) {
String[] companies = { "Yahoo", "Vodafone", "Samsung" };

// sorting java array in ascending order
System.out.println("Sorting String Array in Ascending order in Java Example");
System.out.println("Unsorted String Array in Java: ");
printNumbers(companies);
Arrays.sort(companies);
System.out.println("Sorted String Array in ascending order : ");
printNumbers(companies);

// sorting java array in descending order
System.out.println("Sorting Array in Descending order in Java Example");
System.out.println("Unsorted int Array in Java: ");
printNumbers(companies);
Arrays.sort(companies, Collections.reverseOrder());
System.out.println("Sorted int Array in descending order : ");
printNumbers(companies);

System.out.println("Sorting part of array in java:");
int[] numbers = { 1, 3, 2, 5, 4 };
Arrays.sort(numbers, 0, 3);
System.out.println("Sorted sub array in Java: ");
for (int num : numbers) {
System.out.println(num);
}

}

public static void printNumbers(String[] companies) {
for (String company : companies) {
System.out.println(company);
}
}

}

Output:

Sorting String Array in Ascending order in Java Example
Unsorted String Array in Java:
Yahoo
Vodafone
Samsung

Sorted String Array in ascending order :
Vodafone
Yahoo
Samsung

Sorting Array in Descending order in Java Example
Unsorted int Array in Java:
Vodafone
Yahoo
Samsung
Sorted String Array in descending order :
Samsung
Yahoo
Vodafone
Sorting part of array in java:
Sorted sub array in Java:
1
2
3
5
4

Key Points to remember while Sorting method of Arrays:

The below lists some of the important, significant and key points about sort method of Arrays class for quick references:

Arrays.sort() is considered to be an overloaded method and can help in sorting int, byte, short, char or object[] arrays.

Arrays.sort)( also permits the developers so as to sort a sub-array or part of array in Java.

Arrays class also have the capability to comprise of utility methods such as binarySearch for performing search on array

Conclusion

This is all on the sorting of java array tutorial that helped in arranging java array list in ascending and descending order and part of array in Java. We also learned on the alternative to convert array into array list and then sorting though it is always recommended to make use of Arrays.sort() so as to sort arrays in Java. Hope you liked the article.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login