Sorting in Java

There are two in-built methods to sort in Java.

  1. Arrays.Sort() works for arrays which can be of primitive data type also.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A sample Java program to demonstrate working of
    // Arrays.sort().
    // It by default sorts in ascending order.
    import java.util.Arrays;
      
    public class GFG {
        public static void main(String[] args)
        {
            int[] arr = { 13, 7, 6, 45, 21, 9, 101, 102 };
      
            Arrays.sort(arr);
      
            System.out.printf("Modified arr[] : %s",
                              Arrays.toString(arr));
        }
    }

    chevron_right

    
    

    Output:

    Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]
    
  2. Collections.sort() works for objects Collections like ArrayList and LinkedList.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate working of Collections.sort()
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create a list of strings
            ArrayList<String> al = new ArrayList<String>();
            al.add("Geeks For Geeks");
            al.add("Friends");
            al.add("Dear");
            al.add("Is");
            al.add("Superb");
      
            /* Collections.sort method is sorting the
            elements of ArrayList in ascending order. */
            Collections.sort(al);
      
            // Let us print the sorted list
            System.out.println("List after the use of"
                               + " Collection.sort() :\n" + al);
        }
    }

    chevron_right

    
    

  3. Output:

    List after the use of Collection.sort() :
    [Dear, Friends, Geeks For Geeks, Is, Superb]
    
  • Which sorting algorithm does Java use in sort()?
    Java’s Arrays.sort method uses Quicksort for arrays of primitives and merge sort for arrays of objects.
  • Which order of sorting is done by default?
    It by default sorts in ascending order.
  • How to sort array or list in descending order?
    It can be done with the help of Collections.reverseOrder().

    Example:



    1. For Arrays.sort()
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // A sample Java program to sort an array
      // in descending order using Arrays.sort().
      import java.util.Arrays;
      import java.util.Collections;
        
      public class GFG {
          public static void main(String[] args)
          {
              // Note that we have Integer here instead of
              // int[] as Collections.reverseOrder doesn't
              // work for primitive types.
              Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
        
              // Sorts arr[] in descending order
              Arrays.sort(arr, Collections.reverseOrder());
        
              System.out.printf("Modified arr[] : %s",
                                Arrays.toString(arr));
          }
      }

      chevron_right

      
      

      Output:

      Modified arr[] : [100, 45, 21, 13, 9, 7, 6, 2]
      
    2. For Collections.sort()
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // Java program to demonstrate working of Collections.sort()
      // to descending order.
      import java.util.*;
        
      public class GFG {
          public static void main(String[] args)
          {
              // Create a list of strings
              ArrayList<String> al = new ArrayList<String>();
              al.add("Geeks For Geeks");
              al.add("Friends");
              al.add("Dear");
              al.add("Is");
              al.add("Superb");
        
              /* Collections.sort method is sorting the
              elements of ArrayList in ascending order. */
              Collections.sort(al, Collections.reverseOrder());
        
              // Let us print the sorted list
              System.out.println("List after the use of"
                                 + " Collection.sort() :\n" + al);
          }
      }

      chevron_right

      
      

      Output:

      List after the use of Collection.sort() :
      [Superb, Is, Geeks For Geeks, Friends, Dear]
      
  • How to sort only a subarray?
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A sample Java program to sort a subarray
    // using Arrays.sort().
    import java.util.Arrays;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Our arr contains 8 elements
            int[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
      
            // Sort subarray from index 1 to 4, i.e.,
            // only sort subarray {7, 6, 45, 21} and
            // keep other elements as it is.
            Arrays.sort(arr, 1, 5);
      
            System.out.printf("Modified arr[] : %s",
                              Arrays.toString(arr));
        }
    }

    chevron_right

    
    

    Output:

    Modified arr[] : [13, 6, 7, 21, 45, 9, 2, 100]
    
  • How to write my own sorting function in Java?
    Please see Java programs for Quick Sort, Merge Sort, Insertion Sort, Selection Sort, Heap Sort, Bubble Sort

  • How to sort objects of user defined data type?

    Please refer Arrays.sort() in Java and Collections.sort() in Java for examples.



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.