Array to List, List to Array, Sort List, anonymous class
public static void p( ) {
String[] s = new String[4];
s[0] = "C";
s[1] = "NTM01";
s[2] = "NNTM01";
s[3] = "A";
//Array to List
List sl = Arrays.asList(s);
//anonymous class
Collections.sort(sl, new Comparator() {
//sort list by string length desceding
// --> acdx, asd,cc,cf,s
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
return (s2.length() - s1.length());
}
});
//List to Array
String[] s2 = (String[]) sl.toArray(new String[0]);
System.out.println(s2);
}
0 Comments:
Post a Comment
<< Home