In the development of sometimes need to encapsulate the page sorting, list how to sort a property, share a small example, we encourage each other, hope to be useful for everyone, please advise.
The 1.Student Bean is as follows:
public class Student {
private int age;
private String name;
Private String weight;
Public String Getweight () {return
weight;
}
public void Setweight (String weight) {
this.weight = weight;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
}
2. Sort by the int type attribute of the object in the list
/**
* Sort by the attributes of an int type in the list
* @param list
/@SuppressWarnings ("unchecked") public
static void Sortintmethod (List list) {
collections.sort (list, new Comparator () {
@Override public
int compare (Object O1, Object O2) {
Student stu1= (Student) O1;
Student stu2= (Student) O2;
if (Stu1.getage () >stu2.getage ()) {return
1;
} else if (Stu1.getage () ==stu2.getage ()) {return
0;
} else{
return-1
}}
);
System.out.println ("///////////////after/////////////sort");
for (int i=0;i<list.size (); i++) {
Student st= (Student) list.get (i);
System.out.println ("st.age=" +st.getage () + ", st.name=" +st.getname ());
}
3. Sort by string attributes of the objects in the list
1) Method One:
/**
* Sort by a string attribute in the list
* @param list
/@SuppressWarnings ("unchecked") public
static void Sortstringmethod (List list) {
collections.sort (list, new Comparator () {
@Override public
int Compare ( Object O1, Object O2) {
Student stu1= (Student) O1;
Student stu2= (Student) O2;
Return Stu1.getname (). CompareTo (Stu2.getname ());
}
);
System.out.println ("///////////////after/////////////sort");
for (int i=0;i<list.size (); i++) {
Student st= (Student) list.get (i);
System.out.println ("st.age=" +st.getage () + ", st.name=" +st.getname ());
}
2) Method Two:
Implemented using Java.text.RuleBasedCollator to perform a locale-sensitive string comparison:
/**
* Sort by a string attribute in the list
* @param list
/@SuppressWarnings ("unchecked") public
static void Sortbyrulebasedcollator (List list) {
collections.sort (list, new Comparator () {
@Override public
int Compare (object O1, Object O2) {return
(java.text.RuleBasedCollator) java.text.Collator.getInstance ( Java.util.Locale.CHINA)). Compare ((Student) O1). GetName (), ((Student) O2). GetName ());
System.out.println ("///////////////after/////////////sort");
for (int i=0;i<list.size (); i++) {
Student st= (Student) list.get (i);
System.out.println ("st.age=" +st.getage () + ", st.name=" +st.getname ());
}
4. Test the Sorting method
@SuppressWarnings ("unchecked") public
static void Main (string[] args) {
ArrayList list=new ArrayList ();
Student t1=new Student ();
T1.setage (km);
T1.setname ("Wanglei");
List.add (t1);
Student t2=new Student ();
T2.setage (4);
T2.setname ("Lisi");
List.add (T2);
Student t3=new Student ();
T3.setage (a);
T3.setname ("Zhonghua");
List.add (T3);
Student t4=new Student ();
T4.setage ();
T4.setname ("Waanglei");
List.add (T4);
System.out.println ("/////////////sort before///////////////");
for (int i=0;i<list.size (); i++) {
Student st= (Student) list.get (i);
System.out.println ("st.age=" +st.getage () + ", st.name=" +st.getname ());
}
Sort
Sortintmethod (list) According to the attributes of an int type in the list;
Sort
Sortstringmethod (list) by one of the string attributes in the list;
5. The result