首页 > 代码库 > Collections.sort()用法简单介绍

Collections.sort()用法简单介绍

在开发过程中,在对list集合的排序中遇到了点小阻碍。记录之,与君共勉。

我们先来看看,Collections.sort()的简单用法,代码:

package com.wh.util;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Test {    private static final Logger LOGGER = LoggerFactory.getLogger(Test.class);    public static void main(String[] args) {        List<String> lists = new ArrayList<String>();        lists.add("1");        lists.add("5");        lists.add("3");        LOGGER.info("lists 排序前:" + lists.toString());        //排序,直接调用sort方法排序,排序方式是自燃排序,即升序排序        Collections.sort(lists);        LOGGER.info("lists 排序后:" + lists.toString());    }}

运行结果:

技术分享

完美排序。但是在实际开发中排序的集合远比这个复杂,我们来看简单的模拟。

package com.wh.bean;import java.io.Serializable;/** * Created by WH on 2016/9/17. */public class TestA implements Serializable{    private static final long serialVersionUID = -8968350748602548887L;    private String name;    //变量类型必须要为int的封装类型,因为只有是封装类型才可以调用Integer的方法    private Integer order;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getOrder() {        return order;    }    public void setOrder(int order) {        this.order = order;    }    @Override    public String toString() {        return "TestA{" +                "name=‘" + name + ‘\‘‘ +                ", order=" + order +                ‘}‘;    }}

技术分享

在调用Collections.sort()给listA排序中,编译不通过,报错了。这就是当时我开发中遇到的小阻碍,为什么编译不通过呢,前面的演示中为什么可以排序呢?这是因为lists中的String本身含有compareTo方法,所以可以直接调用sort方法,所以要让类TestA实现Comparable方法,当时参考了网上资料(这里)。解决代码如下:

package com.wh.util;import com.wh.bean.TestA;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Test {    private static final Logger LOGGER = LoggerFactory.getLogger(Test.class);    public static void main(String[] args) {        List<TestA> listA = new ArrayList<TestA>();        //TestA 即是上面类        TestA a1 = new TestA();        a1.setName("张大胖");        a1.setOrder(1);        listA.add(a1);        TestA a5 = new TestA();        a5.setName("王晓梅");        a5.setOrder(5);        listA.add(a5);        TestA a3 = new TestA();        a3.setName("刘晓彤");        a3.setOrder(3);        listA.add(a3);        LOGGER.info("listA 排序前:" + listA.toString());        Collections.sort(listA, new Comparator<TestA>() {            @Override            public int compare(TestA o1, TestA o2) {                //升序                return o1.getOrder().compareTo(o2.getOrder());            }        });        LOGGER.info("listB 升序排序后:" +listA.toString());        Collections.sort(listA, new Comparator<TestA>() {            @Override            public int compare(TestA o1, TestA o2) {                //降序                return o2.getOrder().compareTo(o1.getOrder());            }        });        LOGGER.info("listB 降序排序后:" +listA.toString());    }}

运行结果:

技术分享

这里可以实现降序和升序排序。除了这个解决方法还有另外一个解决方法:

package com.wh.bean;import java.io.Serializable;/** * Created by WH on 2016/9/17. *///实现Comparablepublic class TestB implements Serializable, Comparable<TestB> {    private static final long serialVersionUID = -2872189514594810657L;    private String name;    private Integer order;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getOrder() {        return order;    }    public void setOrder(Integer order) {        this.order = order;    }      @Override    public int compareTo(TestB o) {
     //升序
return this.order.compareTo(o.getOrder()); } @Override public String toString() { return "TestB{" + "name=‘" + name + ‘\‘‘ + ", order=" + order + ‘}‘; }}

 

 

package com.wh.bean;import java.io.Serializable;/** * Created by WH on 2016/9/17. */public class TestB implements Serializable, Comparable<TestB> {    private static final long serialVersionUID = -2872189514594810657L;    private String name;    private Integer order;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getOrder() {        return order;    }    public void setOrder(Integer order) {        this.order = order;    }    @Override    public int compareTo(TestB o) {        return this.order.compareTo(o.getOrder());    }    @Override    public String toString() {        return "TestB{" +                "name=‘" + name + ‘\‘‘ +                ", order=" + order +                ‘}‘;    }}

运行结果:

技术分享

就这样解决了问题。

Collections.sort()用法简单介绍