Table of Contents

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:
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:
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
// A sample Java program to sort an array of integers
// using Arrays.sort(). It by default sorts in
// ascending order
import java.util.Arrays;

public class SortExample
{
public static void main(String[] args)
{
// Our arr contains 8 elements
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.
// 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);

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.
// 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());

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

// 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());

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.
// Java program to demonstrate working of Comparator
// interface
import java.util.*;
import java.lang.*;
import java.io.*;

// A class to represent a student.
class Student
{
int rollno;
String name, subject;

// Constructor
public Student(int rollno, String name,
String subject)
{
this.rollno = rollno;
this.name = name;
this.subject = subject;
}

// 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

Categorized in:

Java,