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


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

Monica Mona
Monica Mona

Student of life, and a lifelong learner

Updated on: 20-Feb-2020

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements