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 firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.highestOneBit() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Arrays.equals() in Java with Examples
- Java.util.Collections.frequency() in Java with Examples
Improved By : Anchit Gupta