/**
* 3 Ways to implement Java object sorting
* @author Zhangwenzhang
*
*/
public class Testobjectsort {
/**
* @param args
*/
public static void Main (string[] args) {
//TODO auto-generated method stub
/** Method 1
* Using Collections.sort (List, Comparator) implementation, must implement a Comparator comparator and replication compare () method
*/
person1[] ps = new person1[]{new Person1 ("P0", 0),
New Person1 ("P1", 3),
New Person1 ("P2", 5),
New Person1 ("P3", 4),
New Person1 ("P4", 8),
New Person1 ("P5", 6),
New Person1 ("P6", 7),
new Person1 ("P7", 1),
new Person1 ("P8", 2),
new Person1 ("P9", 9)};
list<person1> PL = new arraylist<person1> ();
for (int i = 0; i < i++) {
System.out.print (Ps[i].getage ());
Pl.add (Ps[i]);
}
System.out.println ("\ n use Collections.sort (List, Comparator) class to compare:");
long L1 = System.currenttimemillis ();
collections.sort (PL, New mycomparator ());
System.out.println ("Time:" + (System.currenttimemillis ()-L1));
for (Iterator it = Pl.iterator (); It.hasnext ();) {
Person1 p = (Person1) it.next ();
System.out.print (P.getage ());
}
/** Method 2
* Using the Arrays.sort (object[]) implementation, the object must implement the comparable interface and make a replication CompareTo () method
*/
person2[] ps2 = new person2[]{new Person2 ("P0", 0),
New Person2 ("P1", 3),
New Person2 ("P2", 5),
New Person2 ("P3", 4),
New Person2 ("P4", 8),
New Person2 ("P5", 6),
new Person2 ("P6", 7),
new Person2 ("P7", 1),
new Person2 ("P8", 2),
new Person2 ("P9", 9)};
System.out.println ("\ n use Arrays.sort (object[]) class to compare:");
Long L2 = System.currenttimemillis ();
Arrays.sort (PS2);
System.out.println ("Time:" + (System.currenttimemillis ()-L2));
for (int i = 0; i < i++) {
System.out.print (Ps2[i].getage ());
}
/** Method 3
* Using the Collections.sort (List), the object must implement the comparable interface and make a replication CompareTo () method
*/
person2[] ps3 = new Person2[]{new Person2 ("P0", 0),
New Person2 ("P1", 3),
New Person2 ("P2", 5),
New Person2 ("P3", 4),
New Person2 ("P4", 8),
New Person2 ("P5", 6),
New Person2 ("P6", 7),
new Person2 ("P7", 1),
new Person2 ("P8", 2),
new Person2 ("P9", 9)};
list<person2> pl3 = new arraylist<person2> ();
for (int i = 0; i < i++) {
Pl3.add (Ps3[i]);
}
System.out.println ("\ n use Collections.sort (List) class to compare:");
Collections.sort (PL3);
for (Iterator it = Pl3.iterator (); It.hasnext ();) {
Person2 p = (Person2) it.next ();
System.out.print (P.getage ());
}
}
}
/**
* Method 1 requires
* @author Zhangwenzhang
*
*/
Class Mycomparator implements comparator{
public int Compare (object O1, Object O2) {
Person1 p1 = (Person1) O1;
Person1 P2 = (Person1) O2;
if (P1.getage () < P2.getage ()) {
return-1;
}else if (p1.getage () = = P2.getage ()) {
return 0;
}else{
return 1;
}
}
}
/**
* Method 1 requires
* @author Zhangwenzhang
*
*/
class person1{
private String name;
private int age;
public Person1 () {}
public Person1 (String name, int age) {
super ();
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;
}
}
/**
* Method 2,3 need
* @author Zhangwenzhang
*
*/
class Person2 implements comparable{
private String name;
private int age;
public Person2 () {}
public Person2 (String name, int age) {
super ();
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;
}
public int compareTo (Object o) {
Person2 p = (Person2) o;
if (This.age < p.age) {
return-1;
}else if (this.age = = p.age) {
return 0;
}else{
return 1;
}
}
}