Java-Collections的sort方法對list進行排序

來源:互聯網
上載者:User

1.List中的對象實現Comparable介面:

public class User implements Comparable<User>{    private String name;    private Integer order;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getOrder() {        return order;    }    public void setOrder(Integer order) {        this.order = order;    }    public int compareTo(User arg0) {        return this.getOrder().compareTo(arg0.getOrder());    }}

下面是測試類別Test:

import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Test{     public static void main(String[] args) {        User user1 = new User();        user1.setName("a");        user1.setOrder(1);        User user2 = new User();        user2.setName("b");        user2.setOrder(2);              List<User> list = new ArrayList<User>();        //此處add user2再add user1        list.add(user2);        list.add(user1);        Collections.sort(list);        for(User u:list){            System.out.println(u.getName());        }    }}

輸出為:

a

b

2.使用靜態內部類實現Comparator介面,Comparator介面位於java.util包下

import java.util.*;public class Main{public static void main(String args[]){ArrayList al=new ArrayList();al.add(new Student(2,"aa"));al.add(new Student(1,"bb"));al.add(new Student(3,"dd"));al.add(new Student(3,"cc"));Collections.sort(al,new StudentComparator());Iterator it=al.iterator();while(it.hasNext()){System.out.println(it.next());}}}class Student {int id;String name;Student(int id,String name){this.id=id;this.name=name;}public String toString(){return "id="+this.id+",name="+this.name;}}class StudentComparator implements Comparator{public int compare(Object o1,Object o2){Student s1=(Student)o1;Student s2=(Student)o2;int result=(s1.id>s2.id)?1:((s1.id==s2.id)?0:-1);if(0==result){result=s1.name.compareTo(s2.name);}return result;}}

3.補充:我遇到的情況是直接List<String> list=new ArrayList();

在這種情況下,我們如果調用Collections.sort(list);方法,它會對list裡字串從左往右的字母的ascii值進行排序,不用實現Comparator介面。

public class Connectionssort {public static void main(String[] args) {// TODO Auto-generated method stubList<String> list = new ArrayList();int j=0;JDBManager db=new JDBManager();ResultSet rs;String q = "select * from securityEvent";try {rs = db.executeQuery(q);while (rs.next()) {list.add(rs.getString("grade"));}Collections.sort(list);while (j < list.size()) {System.out.println(list.get(j).toString());j++;}}catch (Exception e) {// TODO: handle exception}}}

實驗結果如下:

A

A

B

B

B

C

C

C

D


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.