Sorting in Java refers to the act of arranging the objects in a particular order, be it ascending or descending order. In java, we have predefined methods for sorting the objects. For sorting an array of objects/primitive data types, java defined a method Arrays.sort(). Whereas while working on the  List collections in java (like  LinkedList,  Queue,  ArrayList, etc.), we have a method Collections.sort(). Both of the methods sort the object in the ascending order by default.

If we want to sort the objects in the descending order, we can use the method Collections.reverseOrder().

Sorting in Java

Java Arrays.sort() method is defined in the java.util.Arrays package. This method uses different sorting algorithms in different situations.

For the array of objects it uses mergesort, while when the array is composed of primitive datatypes, it uses quicksort.

See the following syntax.

public static void sort(Object[ ] arr, int start, int end)

Since the return type of this method is void, it does not return anything. arr specifies the array on which we want to apply the sort method. Starttells the starting index from which we want to sort, and end specifies the index up to which we want to perform sort (end excluded).

#java #arrays.sort #quicksort #java.util.arrays

Sorting in Java Example | Java Sorting Tutorial
1.55 GEEK