There are two ways to sort a property of an object in a collection.
A. One is to sort the object class to implement the comparable interface CompareTo method, then put the object into the list, then call Collections.sort (list);
B. One is not to make any changes to the Sort object class, create the implementation class C of the comparator interface, then put the object into the list, and then call Collections.sort (list, C);
A.eg
----------------------------------
public class User implements comparable<user>{
private String name;
private int age;
private int gender;
Private String address;
/**
* @return The name
*/
Public String GetName () {
return name;
}
/**
* @param name the name to set
*/
public void SetName (String name) {
THIS.name = name;
}
/**
* @return The Age
*/
public int getage () {
return age;
}
/**
* @param age of the age to set
*/
public void Setage (int age) {
This.age = age;
}
/**
* @return The Gender
*/
public int Getgender () {
return gender;
}
/**
* @param gender the gender to set
*/
public void Setgender (int gender) {
This.gender = gender;
}
/**
* @return The Address
*/
Public String getaddress () {
return address;
}
/**
* @param address the address to set
*/
public void setaddress (String address) {
this.address = address;
}
/**
*
*/
Public User () {
Super ();
}
/**
* @param name
* @param age
* @param gender
* @param address
*/
Public User (string name, int age, int gender, string address) {
Super ();
THIS.name = name;
This.age = age;
This.gender = gender;
this.address = address;
}
@Override
public int compareTo (User o) {
if (o!=null) {
if (This.getage () >o.getage ()) {
return 1;
}else if (This.getage () ==o.getage ()) {
return 0;
}
}
return-1;
}
}
-------------------------------------------Main
list<user> ulist = new arraylist<user> ();
Ulist.add (New User ("Wangbo", 29, 1, "Changsha"));
Ulist.add (New User ("Wumei", 44, 1, "Zhuzhou"));
Ulist.add (New User ("Zhangjie", 19, 1, "Yueyang"));
Ulist.add (New User ("Lihua", 36, 1, "Changsha"));
Ulist.add (New User ("Zhangchangzhe", 19, 1, "Hengyang"));
Collections.sort (list);
for (User u:ulist) {
System.out.println (U.getname () + "T" +u.getage () + "T" +u.getaddress ());
}
==============================================
B.eg
--------------------------------
public class User {
private String name;
private int age;
private int gender;
Private String address;
/**
* @return The name
*/
Public String GetName () {
return name;
}
/**
* @param name the name to set
*/
public void SetName (String name) {
THIS.name = name;
}
/**
* @return The Age
*/
public int getage () {
return age;
}
/**
* @param age of the age to set
*/
public void Setage (int age) {
This.age = age;
}
/**
* @return The Gender
*/
public int Getgender () {
return gender;
}
/**
* @param gender the gender to set
*/
public void Setgender (int gender) {
This.gender = gender;
}
/**
* @return The Address
*/
Public String getaddress () {
return address;
}
/**
* @param address the address to set
*/
public void setaddress (String address) {
this.address = address;
}
/**
*
*/
Public User () {
Super ();
}
/**
* @param name
* @param age
* @param gender
* @param address
*/
Public User (string name, int age, int gender, string address) {
Super ();
THIS.name = name;
This.age = age;
This.gender = gender;
this.address = address;
}
}
------------------------------
Import Java.util.Comparator;
public class Comparatorimpl implements comparator<user>{
@Override
public int Compare (user O1, user O2) {
if (null!=o1 && null!=o2) {
if (O1.getage () >o2.getage ()) {
return 1;
}else if (O1.getage () ==o2.getage ()) {
return 0;
}
}
return-1;
}
}
-----------------------------------Main
list<user> ulist = new arraylist<user> ();
Ulist.add (New User ("Wangbo", 29, 1, "Changsha"));
Ulist.add (New User ("Wumei", 44, 1, "Zhuzhou"));
Ulist.add (New User ("Zhangjie", 19, 1, "Yueyang"));
Ulist.add (New User ("Lihua", 36, 1, "Changsha"));
Ulist.add (New User ("Zhangchangzhe", 19, 1, "Hengyang"));
Collections.sort (Ulist, New Comparatorimpl ());
for (User u:ulist) {
System.out.println (U.getname () + "T" +u.getage () + "T" +u.getaddress ());
}