Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sort Array Using sort(Object[], int, int) Method 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
Updated on: 2020-02-20T12:34:40+05:30

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements