What is an Array?
- 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:
- 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:
String[] subject = {"HTML", "CSS", "PHP", "PYTHON", "JAVA"};
Arrays.sort() in java
- The sorting may be a way to arrange elements of a list or array during a certain order.
- The order could also be in ascending or descending order.
- The numerical and lexicographical (alphabetical) order may be a widely used order.
- sort() method is a java.util.Arrays class method.
Syntax
public static void sort(int[] arr, int from_Index, int to_Index)
- arr – The array to be sorted.
- from_Index – The indexof 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.
Sort an array of integers in ascending order.
- The ascending order arranges the elements within the lowest to highest order. It’s also referred to as natural order or numerical order
import java.util.Arrays;
public class SortExample
{
public static void main(String[] args)
{
int[] arr = {8, 4, 70, 2, 18, 26, 100, 85};
Arrays.sort(arr);
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Output
Modified arr[] : [2, 4, 8, 18, 26, 70, 85, 100]
Use sort() to sort a subarray of arr[]
- 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.
import java.util.Arrays;
public class SortExample
{
public static void main(String[] args)
{
int[] arr = {13, 7, 6, 45, 21, 9, 2, 100};
Arrays.sort(arr, 1, 5);
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Output
Modified arr[] : [13, 6, 7, 21, 45, 9, 2, 100]
Sort Array in Descending Order
- The descending order arranges the elements within the highest to lowest order.
import java.util.Arrays;
import java.util.Collections;
public class SortExample
{
public static void main(String[] args)
{
Integer[] arr = {8, 4, 70, 2, 18, 26, 100, 85};
Arrays.sort(arr, Collections.reverseOrder());
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Output
Modified arr[] : [100, 85, 70, 26, 18, 8, 4, 2]
We can also sort strings in alphabetical order
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"
};
Arrays.sort(arr);
System.out.printf("Modified arr[] : \n%s\n\n",
Arrays.toString(arr));
Arrays.sort(arr, Collections.reverseOrder());
System.out.printf("Modified arr[] : \n%s\n\n",
Arrays.toString(arr));
}
}
Output
Modified arr[] :
Modified arr[] :
[quiz.wikitechy.org, practice.wikitechy.org, code.wikitechy.org]
We can also sort an array according to user defined criteria
- We use Comparator interface for this purpose.
import java.util.*;
import java.lang.*;
import java.io.*;
class Student
{
int rollno;
String name, subject;
public Student(int rollno, String name,
String subject)
{
this.rollno = rollno;
this.name = name;
this.subject = subject;
}
public String toString()
{
return this.rollno + " " + this.name +
" " + this.subject;
}
}
class Sortbyroll implements Comparator<Student>
{
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
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