简体   繁体   English

使用比较器对列表名称和日期进行排序

[英]sorting a list name and date using comparator

I am trying to implement custom sorting using comparator. 我正在尝试使用比较器实现自定义排序。 So far I was able to sort a list in an order where specified name on the top of the list and the rest follows. 到目前为止,我已经能够按照指定的名称在列表顶部的顺序对列表进行排序,其余的紧随其后。

Code: 码:

public class SortingTest implements Comparator<SortingTest> {
 String name;
 Date date;

 public SortingTest(String name, Date date) {
    this.name = name;
    this.date = date;
 }

@Override
public int compare(SortingTest o1, SortingTest o2) {
    if (this.name.equals(o2.name)) {
        return 1;
    }
    return 0;
}

public static void main(String[] args) {
    List<SortingTest> list = new ArrayList<SortingTest>();
    Calendar cl = Calendar.getInstance();
    Date d = cl.getTime();
    SortingTest s1 = new SortingTest("Sas", d);
    cl.add(Calendar.DATE, 1);
    Date d1 = cl.getTime();
    SortingTest s2 = new SortingTest("Dave", d1);
    cl.add(Calendar.DATE, 1);
    Date d2 = cl.getTime();
    SortingTest s3 = new SortingTest("Jabir", d2);
    cl.add(Calendar.DATE, 1);
    Date d4 = cl.getTime();
    SortingTest s5 = new SortingTest("Meina", d4);
    cl.add(Calendar.DATE, 1);
    Date d5 = cl.getTime();
    SortingTest s6 = new SortingTest("Sas", d5);
    list.add(s1);
    list.add(s2);
    list.add(s3);
    list.add(s5);
    list.add(s6);

    Collections.sort(list, s1);

    System.out.println("After sorting");
    for (SortingTest st : list) {
        System.out.println(st.name);
    }
}
}

Current code Output: 当前代码输出:

Sas
Sas
Dave
Jabir
Meina

But I would like to sort the list first by name and then by date. 但我想先按名称对列表进行排序,然后再按日期对列表进行排序。 So the output should be: 因此输出应为:

Sas, 8/6/2015
Sas, 14/6/2015
Dave, 8/6/2015
Jabir, 10/6/2015
Meina, 11/6/205

Updated 1: 更新1:

My criteria is that I should be able to pass any name (in this case "sas"/ s1) and sort the list so that the passed name should be on top of the list and then the rest. 我的标准是我应该能够传递任何名称(在本例中为“ sas” / s1)并对列表进行排序,以便传递的名称应位于列表的顶部,然后位于其余的顶部。 So in the above example I have passed an object s1 to collection.sort method. 因此,在上面的示例中,我已将对象s1传递给collection.sort方法。 s1 holds the name "sas" and in compare method I am checking if any other element in the list has name "sas" then give value 1 (top of the list) or 0 if it's not. s1拥有名称“ sas”,在比较方法中,我正在检查列表中是否有其他元素具有名称“ sas”,然后给出值1(在列表顶部),如果没有则给出0。

First of all, you could implement Comparable instead of Comparator and it would allow you to use Collections.sort(list) instead of Collections.sort(list, s1) . 首先,您可以实现Comparable而不是Comparator ,这将允许您使用Collections.sort(list)而不是Collections.sort(list, s1)

Now, personally, I don't like changing the core "comparable" requirements of object, as they may have been defined that way for a reason, instead, I like to create Comparator based on the immediate needs instead, for example.. 现在,就我个人而言,我不喜欢更改对象的核心“可比”需求,因为它们可能是出于某种原因而定义的,例如,我更喜欢根据当前需求创建Comparator

Collections.sort(list, new Comparator<SortingTest>() {
    @Override
    public int compare(SortingTest o1, SortingTest o2) {
        int result = o1.name.compareTo(o2.name);
        if (result == 0) {
            result = o1.date.compareTo(o2.date);
        } else {
            result = -result;
        }
        return result;
    }
});

This basically compares the name of each SortingTest instance, if they are 0 , it will then use the Date to determine the difference. 这基本上是比较每个SortingTest实例的名称,如果它们是0 ,则它将使用Date来确定差异。

This would then output something like... 然后将输出类似...

Sas Thu Aug 07 12:30:11 EST 2014
Sas Mon Aug 11 12:30:11 EST 2014
Meina Sun Aug 10 12:30:11 EST 2014
Jabir Sat Aug 09 12:30:11 EST 2014
Dave Fri Aug 08 12:30:11 EST 2014

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM