Http://hi.baidu.com/pjww/blog/item/01741d7fa563210a28388a95.html
Import java. Io .*;
Import java. util .*;
Public class main {
Public static void main (string ARGs []) throws exception {
Cin = new partition (system. In );
String STR;
Int N;
STR = cin. Next ();
N = cin. nextint ();
Cin. nextline ();
List <string> mylist = new arraylist <string> (N );
For (INT I = 0; I <n; I ++)
Mylist. Add (CIN. nextline (). Trim ());
If (Str. compareto ("Inc") = 0) // natural sequence
Collections. Sort (mylist, new comparator <string> (){
Public int compare (string a, string B ){
Return A. compareto (B );
}
});
If (Str. compareto ("dec") = 0) // natural Reverse Order (DEC)
Collections. Sort (mylist, new comparator <string> (){
Public int compare (string a, string B ){
Return-A. compareto (B );
}
});
If (Str. compareto ("ncinc") = 0) // ignore the case sensitivity sequence (ncinc)
Collections. Sort (mylist, new comparator <string> (){
Public int compare (string a, string B ){
Return A. comparetoignorecase (B );
}
});
If (Str. compareto ("ncdec") = 0) // ignore the case Reverse Order (ncdec)
Collections. Sort (mylist, new comparator <string> (){
Public int compare (string a, string B ){
Return-A. comparetoignorecase (B );
}
});
Iterator <string> Te = mylist. iterator ();
While (TE. hasnext () {// traverse the output
System. Out. println (TE. Next ());
}
}
}
Http://blog.sina.com.cn/s/blog_5f17b8e40100g0g7.html
Package test. Sort;
Import java. util. arraylist;
Import java. util. comparator;
Import java. util. List;
Public class test {
Public void test1 (){
List <string> List = new arraylist <string> ();
List. Add ("D ");
List. Add ("e ");
List. Add ("B ");
List. Add ("C ");
List. Add ("");
System. Out. println (list. Get (0 ));
System. Out. println (list. Get (1 ));
System. Out. println (list. Get (2 ));
System. Out. println (list. Get (3 ));
System. Out. println (list. Get (4 ));
}
Public static void main (string [] ARGs ){
Person P3 = new person ("careers3", 33 );
Person P4 = new person ("careers4", 44 );
Person p1 = new person ("careers1", 11 );
Person P2 = new person ("careers2", 22 );
List <person> List = new arraylist <person> ();
List. Add (P3); list. Add (P4); list. Add (P1); list. Add (P2 );
System. Out. println (list. Get (0). getage ());
System. Out. println (list. Get (1). getage ());
System. Out. println (list. Get (2). getage ());
System. Out. println (list. Get (3). getage ());
System. Out. println ("===================== ");
Java. util. Collections. Sort (list, new comparator (){
Public int compare (Object obj1, object obj2 ){
Person A = (person) obj1;
Person B = (person) obj2;
Int age1 = A. getage ();
Int age2 = B. getage ();
If (age1 = age2) {return 0 ;}
If (age1> age2) {return 1 ;}
Return-1;
}
});
System. Out. println (list. Get (0). getage ());
System. Out. println (list. Get (1). getage ());
System. Out. println (list. Get (2). getage ());
System. Out. println (list. Get (3). getage ());
}
}
Class person {
Private string name;
Private int age;
Public Person (string name, int age ){
This. Name = Name;
This. Age = age;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
}