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

Java 8Object Oriented ProgrammingProgramming

The java.util.Arrays.sort(Object[] a, int fromIndex, int toIndex) method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. 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) {
      Object ob[] = {27, 11, 5, 44};

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

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

Output

Number = 27
Number = 11
Number = 5
Number = 44
Object array with some sorted values(1 to 3) is:
Number = 27
Number = 5
Number = 11
Number = 44
raja
Published on 16-Jan-2018 11:02:00
Advertisements