What does the method sort(obj[] a) do in java?


The sort(Object[]) method of the java.util.Arrays class sorts the specified array of Objects into ascending order according to the natural ordering of its elements.

Example

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      Object ob[] = {27, 11, 44};
      for (Object number : ob) {
         System.out.println("Number = " + number);
      }
      Arrays.sort(ob);
      System.out.println("The sorted Object array is:");

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

Output

Number = 27
Number = 11
Number = 44
The sorted Object array is:
Number = 11
Number = 27
Number = 44

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 25-Feb-2020

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements