Arrays sort in Java

At one time, sorting array was often a challenge for new developers. Hey,it was also often a pain in the neck for us experienced developers! Well , not anymore. You now have another method (function)available to you: the Arrays.sort method(Arrays.sort()). This method sorts the contents of an array according to its natural ordering, which is essentially the common ordering that you would expect. Numbers are sorted in increasing order, and String objects are sorted alphabetically(a,b,c,d). You will find this method helpful when you have a String array that you want to ensure is in alphabetic order.

 

                All you have to pass to the Array.sort() method is the array you want to sort. Once again, you can pass any type of array.

     The following code creates a String[](String array) and then sorts it9ascending order) alphabetically.

import java.util.*;

class ArraySort

{

public static void main(String aa[])

                {

                                String[] names={"Lee","Geet","Suzzanne","Seon"};

                                Arrays.sort(names);

                                for(int i=0;i<names.length;i++)

                                {

                                                System.out.println(names[i]);

                                }

                }

}

Note:

A process used in sorting algorithms to determine how a specific set of data should be ordered. This is normally an ascending order for numbers and alphabetic order for characters and strings.

Output…..

When you run this code, you will see the names in alphabetic order like this:

Arrays sort in Java

With this method , it is that easy to sort arrays, and the performance is quite acceptable as well. Of course, the larger the array, the more time it is going to take to sort it; so don’t expect an array of 1,000 elements to sort as fast as an array of 6. Using this method sure beats writing your own sort algorithms for simple sorting like this!

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @