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{
Scanner cin = new Scanner (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 order
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 Case order (Ncinc)
Collections.sort (MyList, New comparator<string> () {
public int Compare (string A, string b) {
return a.comparetoignorecase (b);
}
});
if (Str.compareto ("ncdec") = = 0)//Ignore 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 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 ("A");
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;
}
}