What does the method sort(int[] a, int fromIndex, int toIndex) do in java?


The sort(int[] a, int fromIndex, int toIndex) method of the java.util.Arrays class sorts the specified range of the specified array of integer value into ascending numerical order. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive.

Example

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      int iArr[] = {3, 1, 2, 18, 10};

      for (int number : iArr) {
         System.out.println("Number = " + number);
      }
      Arrays.sort(iArr, 0, 3);
      System.out.println("int array with some sorted values(0 to 3) is:");

      for (int number : iArr) {
         System.out.println("Number = " + number);
      }
   }
}

Output

Number = 3
Number = 1
Number = 2
Number = 18
Number = 10
int array with some sorted values(0 to 3) is:
Number = 1
Number = 2
Number = 3
Number = 18
Number = 10

Updated on: 20-Feb-2020

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements