sort() method is a java.util.Arrays class method.
public static void sort(int arr, int from_Index, int to_Index) arr - the array to be sorted from_Index - the index of the first element, inclusive, to be sorted to_Index - the index of the last element, exclusive, to be sorted This method doesn't return any value.
A Java program to sort an array of integers in ascending order.
Modified arr : [6, 7, 9, 13, 21, 45, 101, 102]
We can also use sort() to sort a subarray of arr
Modified arr : [13, 6, 7, 21, 45, 9, 2, 100]
We can also sort in descending order.
Modified arr : [100, 45, 21, 13, 9, 7, 6, 2]
We can also sort strings in alphabetical order.
Modified arr : Modified arr : [quiz.geeksforgeeks.org, practice.geeksforgeeks.org, code.geeksforgeeks.org]
We can also sort an array according to user defined criteria.
We use Comparator interface for this purpose. Below is an example.
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 121 cccc jaipur 131 aaaa nyc
This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- Sort a String in Java (2 different ways)
- Reverse a string in Java
- Arrays in Java
- Arrays.binarySearch() in Java with examples | Set 1
- Arrays.toString() in Java with Examples
- Arrays copyOf() in Java with examples
- Collections.sort() in Java with Examples
- Split() String method in Java with examples
- Arrays.fill() in Java with Examples
- ArrayList in Java
- Arrays class in Java
- Comparator Interface in Java with Examples
- Collections in Java
- Scanner Class in Java
- BigInteger Class in Java
Improved By : Anchit Gupta