Answers for "order array java"

1

Arrays.sort() in java

// java sort arraylist
import java.util.ArrayList;
import java.util.Collections;
public class JavaSortArraylist 
{
   public static void main(String[] args) 
   {
      ArrayList<String> al = new ArrayList<String>();
      al.add("Bear"); 
      al.add("Fox"); 
      al.add("Deer"); 
      al.add("Cheetah"); 
      al.add("Anteater");
      al.add("Elephant");
      System.out.println("Before sorting ArrayList: " + al);
      Collections.sort(al); 
      System.out.println("After sorting ArrayList in Ascending order: " + al);
   }
}
Posted by: Guest on November-29-2020
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);
      }
   }
}
Posted by: Guest on November-29-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language