Sometimes when you need to encapsulate and sort your own page, how does List sort a certain attribute? Share a small example, everyone encourages you, I hope it will be useful to you. Please give me some advice.
1. Student's 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 List
/** * Sort by the attribute of an Int type in List* @param list */@SuppressWarnings("unchecked")public static void sortIntMethod(List list){ Collections.sort(list, new Comparator(){@Overridepublic 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("/////////////排序之后///////////////"); 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 the properties of the String type of the object in List
1) Method 1:
/** * Sort by the attribute of a String type in List* @param list */@SuppressWarnings("unchecked")public static void sortStringMethod(List list){ Collections.sort(list, new Comparator(){@Overridepublic int compare(Object o1, Object o2) {Student stu1=(Student)o1;Student stu2=(Student)o2;return stu1.getName().compareTo(stu2.getName());} }); System.out.println("///////////////////////////////////////"); 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 2:
Implemented using java.text.RuleBasedCollator to perform String comparisons that distinguish locale:
/** * Sort by attributes of a String type in List* @param list */@SuppressWarnings("unchecked")public static void sortByRuleBasedCollator(List list){Collections.sort(list, new Comparator(){@Overridepublic 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("/////////////排序之后///////////////"); 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(35);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(56);t3.setName("zhonghua");list.add(t3);Student t4=new Student();t4.setAge(39);t4.setName("waanglei");list.add(t4);System.out.println("/////////////排序之前///////////////");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 according to the properties of Int type in List sortIntMethod(list); //Sort according to the properties of String type in List sortStringMethod(list);} 5. Results