Thursday, October 13, 2011

Sort Array

java.util.Arrays can be used to sort a array.

Example:
Sort Array

import java.lang.String;
import java.util.Arrays;

public class exSortArray{

public static void main(String[] args){

String src[] ={
"MNO",
"A",
"DEF",
"GHI",
"JKL",
"PQR",
"BC",
"STU",
"YZ",
"VWX"};

System.out.println("Sort Array Exerecise");
System.out.println();

System.out.println("Array before Sort");
showAll(src);

System.out.println();

Arrays.sort(src);
System.out.println("Array after Sort");
showAll(src);

}

static void showAll(String[] a){
for(int i = 0; i < a.length; i++){
System.out.println(i + ": " + a[i]);
}
}


}



Related Article:
- Sort array of object using custom Comparator



No comments:

Post a Comment