java ArrayList集合中的某個對象屬性進行排序的實現代碼_java

來源:互聯網
上載者:User

開發中有時候需要自己封裝分頁排序時,List如何對某一屬性排序呢,分享一個小執行個體,大家共勉,希望能對大家有用,請多多指教。

1.Student的Bean如下:

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.按照List中對象的Int類型屬性進行排序

 /** * 按照List中的某個Int類型的屬性進行排序 * @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.按照List中對象的String類型的屬性進行排序

1)方法一:

 /** * 按照List中的某個String類型的屬性進行排序 * @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)方法二:
使用java.text.RuleBasedCollator來實現,用來執行區分語言環境的String 比較:

/** * 按照List中的某個String類型的屬性進行排序 * @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.測試排序的方法

    @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());  }        //按照List中的某個Int類型的屬性進行排序sortIntMethod(list);//按照List中的某個String類型的屬性進行排序sortStringMethod(list);}

5.結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.