sort method for an array in Java code example

Example 1: sort array java

import java. util. Arrays;
Arrays. sort(array);

Example 2: Arrays.sort() in java

// java program to sort strings in an alphabetical order
import java.util.Arrays;
public class SortStringAlphabeticalOrder
{
   public static void main(String[] args)
   {
      String[] strAsc = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"};
      System.out.println("Before sorting: ");
      for(String string : strAsc)
      {
         System.out.println(string);
      }
      // arrays.sort
      Arrays.sort(strAsc);
      System.out.println("-----------------------------------");
      System.out.println("After sorting: ");
      for(String str : strAsc)
      {
         System.out.println(str);
      }
   }
}

Tags:

Java Example