Which of the following methods is used to sort an array in java ________? code example
Example 1: Arrays.sort() in java
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(strAsc);
System.out.println("-----------------------------------");
System.out.println("After sorting: ");
for(String str : strAsc)
{
System.out.println(str);
}
}
}
Example 2: Arrays.sort() in java
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);
}
}