Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
String[] subject;
We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal – place the values in a comma-separated list, inside curly braces:
An array derived from the array is understood as subarray. Suppose, a[] is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and that we want to sort array elements from 34 to 18. It’ll sort the subarray [34, 2, 45, 3, 22, 18] and keep the other elements because it is.
To sort the subarray, the Arrays class provides the static method named sort(). It sorts the required range of the array into ascending order. We will also sort the array of type long, double, float, char, byte, etc.
Syntax
public static void sort(int[] a, int fromIndex, int toIndex)
The method parses the following three parameters:
a: An array to be sort.
fromIndex: The index of the primary element of the subarray. It participates within the sorting.
toIndex: The index of the last element of the subarray. It doesn’t participate within the sorting.
// A sample Java program to sort a subarray // using Arrays.sort(). import java.util.Arrays;
public class SortExample { 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);
The descending order arranges the elements within the highest to lowest order.
// A sample Java program to sort a subarray // in descending order using Arrays.sort(). import java.util.Arrays; import java.util.Collections;
public class SortExample { 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 = {8, 4, 70, 2, 18, 26, 100, 85};
// Sorts arr[] in descending order Arrays.sort(arr, Collections.reverseOrder());
// A sample Java program to sort an array of strings // in ascending and descending orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections;
public class SortExample { public static void main(String[] args) { String arr[] = {"practice.wikitechy.org", "quiz.wikitechy.org", "code.wikitechy.org" };
// Sorts arr[] in ascending order Arrays.sort(arr); System.out.printf("Modified arr[] : \n%s\n\n", Arrays.toString(arr));
// Sorts arr[] in descending order Arrays.sort(arr, Collections.reverseOrder());
// Used to print student details in main() public String toString() { return this.rollno + " " + this.name + " " + this.subject; } }
class Sortbyroll implements Comparator<Student> { // Used for sorting in ascending order of // roll number public int compare(Student a, Student b) { return a.rollno - b.rollno; } }
// Driver class class Main { public static void main (String[] args) { Student [] arr = {new Student(111, "bbbb", "html"), new Student(131, "aaaa", "css"), new Student(121, "cccc", "php")};
System.out.println("Unsorted"); for (int i=0; i<arr.length; i++) System.out.println(arr[i]);
Arrays.sort(arr, new Sortbyroll());
System.out.println("\nSorted by rollno"); for (int i=0; i<arr.length; i++) System.out.println(arr[i]); } }
Output
Unsorted 111 bbbb html 131 aaaa css 121 cccc php
Sorted by rollno 111 bbbb html 121 cccc php 131 aaaa css
Java vs JavaScript Introduction Java and JavaScript are both programming languages, but they have different purposes and features. Java is a general-purpose…