Package com;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;
Import Java.util.Date;
Import java.util.List;
/**
* Sort the list collection
* @author Gouwei
*
*/
public class Sortlist<e> {
@SuppressWarnings ("Unchecked")
public void sort (list<e> List, final string method, final string sort) {
if (list! = null) {
Collections.sort (list, new Comparator () {
public int Compare (object A, object B) {
int ret = 0;
try{
Method M1 = ((E) a). GetClass (). GetMethod (method, NULL);
Method m2 = ((E) b). GetClass (). GetMethod (method, NULL);
if (sort! = null && "desc". Equals (sort))//Reverse
ret = M2.invoke ((((e) b), null). ToString (). CompareTo (M1.invoke ((((e) a), null). ToString ());
else//Positive Sequence
ret = M1.invoke ((((e) a), null). ToString (). CompareTo (M2.invoke ((((e) b), null). ToString ());
}catch (nosuchmethodexception ne) {
System.out.println (NE);
}catch (Illegalaccessexception IE) {
System.out.println (IE);
}catch (InvocationTargetException it) {
SYSTEM.OUT.PRINTLN (IT);
}
return ret;
}
});
}
}
/**
* @param args
*/
public static void Main (string[] args) {
list<userinfo> list = new arraylist<userinfo> ();
SimpleDateFormat formater = new SimpleDateFormat ("Yyyy-mm-dd");
try {
List.add (New UserInfo (3, "B", Formater.parse ("1980-12-01"), 1d, "09:20"));
List.add (New UserInfo (100011, "C", Formater.parse ("1980-10-01"), 989d, "19:10"));
List.add (New UserInfo (2, "a", Formater.parse ("1973-10-01"), 10d, "15:30"));
} catch (ParseException e) {
E.printstacktrace ();
}
System.out.println ("-------Original Sequence-------------------");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.geta ());
}
Call sort General Class
sortlist<userinfo> sortlist = new sortlist<userinfo> ();
//
Sort by a
Sortlist.sort (list, "Geta", "desc");
System.out.println ("--------by a reverse------------------");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.geta ());
// }
Sort by a
Sortlist.sort (list, "Geta", null);
System.out.println ("--------by a positive sequence------------------");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.geta ());
// }
//
Sort by B
Sortlist.sort (list, "Getb", "desc");
System.out.println ("---------Sort by B-----------------");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.getb ());
// }
//
Sort by Time
Sortlist.sort (list, "GetTime", null);
System.out.println ("---------Sort-----------------by Time");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.gettime ());
// }
Sort by C
Sortlist.sort (list, "GetC", null);
System.out.println ("---------Sort by C-----------------");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.getc ());
// }
System.out.println ("---------in reverse order of C-----------------");
SORTC (list, "desc");
Sort by C
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.getc ());
// }
System.out.println ("---------by C ascending-----------------");
SORTC (list, "ASC");
Sort by C
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.getc ());
// }
System.out.println ("---------in reverse order of D-----------------");
SortD (list, "desc");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.getd ());
}
System.out.println ("---------by D ascending order-----------------");
SortD (list, "ASC");
for (UserInfo user:list) {
System.out.println (user.tostring () + ":" +user.getd ());
}
}
public static void Sortc (List list,string type) {
if ("ASC". Equals (type)) {
Collections.sort (list, new Comparator () {
@Override
public int Compare (object O1, Object O2) {
Double a = ((UserInfo) O1). GetC ();
Double B = ((UserInfo) O2). GetC ();
return (int) (A-B);
}
});
}else{
Collections.sort (list, new Comparator () {
@Override
public int Compare (object O1, Object O2) {
Double a = ((UserInfo) O1). GetC ();
Double B = ((UserInfo) O2). GetC ();
return (int) (B-A);
}
});
}
}
public static void SortD (List list,string type) {
if ("ASC". Equals (type)) {
Collections.sort (list, new Comparator () {
@Override
public int Compare (object O1, Object O2) {
String a = ((UserInfo) O1). GETD ();
int a1 = integer.valueof (A.split (":") [0]);
String B = ((UserInfo) O2). GETD ();
int B1 = integer.valueof (B.split (":") [0]);
return A1-B1;
}
});
}else{
Collections.sort (list, new Comparator () {
@Override
public int Compare (object O1, Object O2) {
String a = ((UserInfo) O1). GETD ();
int a1 = integer.valueof (A.split (":") [0]);
String B = ((UserInfo) O2). GETD ();
int B1 = integer.valueof (B.split (":") [0]);
return B1-A1;
}
});
}
}
}
Class userinfo{
private int A;
Private String B;
Private Date time;
Private Double C;
Private String D;
public int Geta () {
return A;
}
public void SetA (int a) {
THIS.A = A;
}
Public String Getb () {
return b;
}
public void Setb (String b) {
this.b = b;
}
Public Date GetTime () {
return time;
}
public void SetTime (Date time) {
This.time = time;
}
Public Double GetC () {
return C;
}
public void SetC (Double c) {
THIS.C = C;
}
Public String getd () {
return D;
}
public void setd (String d) {
THIS.D = D;
}
Public UserInfo (int A, string B, Date time, Double C, string d) {
Super ();
THIS.A = A;
this.b = b;
This.time = time;
THIS.C = C;
THIS.D = D;
}
}
Sort by the properties of an object