package com.java8s.sort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class JavaObjectSortingExample {
/**
*
This class shows how to sort primitive arrays,
*
Wrapper classes Object Arrays
*
@param args
*/
public static void main(String[] args) {
//sort primitives
array like int array
int[] ar = {50,100,40,30};
Arrays.sort(ar);
System.out.println(Arrays.toString(ar));
//sorting String array
String[] arr = {"A", "C", "B", "Z", "E"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
//sorting list of
objects of Wrapper classes
List<String> l = new ArrayList<String>();
l.add("A");
l.add("C");
l.add("B");
l.add("Z");
l.add("E");
Collections.sort(l);
for(String str: l) System.out.print(" "+str);
}
}
Output
[30,40,50,100]
[A,B,C,E,Z]
A B C E Z
No comments:
Post a Comment