简体   繁体   中英

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:

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. So in the above example I have passed an object s1 to collection.sort method. 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.

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) .

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..

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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