-1

i want to sort the list of task,first by date and then by taskID below is the code

ArrayList<fullist> taskdet = new ArrayList<fullist>();
public static class fullist
      {        

 public int date;
 public int id;

 public fullist(int id, int date) {

        this.date = date;
        this.id = id;
    }



}

i used the Collections.sort method to sort but through this i can sort either by date or ID at a time. if sort by date

Collections.sort(taskdet, new Comparator<fullist>() {

        @Override
        public int compare(fullist o1, fullist o2) {
            //if(o1)
            return Integer.compare(o1.date, o2.date);
        }
    });

This is the output displayed :

 day 19 ID 2
 day 19 ID 1
 day 19 ID 3
 day 20 ID 2
 day 20 ID 1
 day 20 ID 3

if sort by ID.

Collections.sort(taskdet, new Comparator<fullist>() {

        @Override
        public int compare(fullist o1, fullist o2) {
            //if(o1)
            return Integer.compare(o1.date, o2.date);
        }
    });

This is the output displayed :

 day 20 ID 1
 day 19 ID 1
 day 19 ID 2
 day 20 ID 2
 day 20 ID 3
 day 19 ID 3

but output should be :

day 19 ID 1
day 19 ID 2
day 19 ID 3
day 20 ID 1
day 20 ID 2
day 20 ID 3
Larbie
  • 1
  • 2
  • This is probably the answer you are looking for: http://stackoverflow.com/a/369867/1008519 – mlunoe Jun 15 '15 at 21:37
  • collections.sort is stable, so you can sort successively. Also you can find how you want to compare you 2 objects so that it does what you want in your comparator. – njzk2 Jun 15 '15 at 22:01

5 Answers5

2

If you use Java 8 you can write:

taskdet.sort(comparingInt(fullist::getDate).thenComparingInt(fullist::getId));

This assumes you have a getDate and getId getters and this static import:

import static java.util.Comparator.comparingInt;
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    If we have Java 8 we can simply write `list.sort(comparator)` instead of `Collections.sort(list,comparator)`. – Pshemo Jun 15 '15 at 21:31
  • 1
    Consider using `comparingInt(...).thenComparingInt(...)` since you're dealing with primitives.. – Alexis C. Jun 15 '15 at 21:45
2

An other nice solution is to use Guava ComparisonChain:

public int compare(fullist o1, fullist o2) {
    return ComparisonChain.start()
                          .compare(o1.date, o2.date)
                          .compare(o1.id, o2.id)
                          .result();
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

You need your Comparator to compare the id values if the date values are the same.

@Override
public int compare(fullist o1, fullist o2) {
    int comp = Integer.compare(o1.date, o2.date);
    if (comp == 0)
    {
        comp = Integer.compare(o1.id, o2.id);
    }
    return comp;
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

This will work:

Collections.sort(taskdet, new Comparator<Fullist>() {

        @Override
        public int compare(fullist o1, fullist o2) {                
            int res = Integer.compare(o1.date, o2.date);
            if(res == 0)
                return o1.id - o2.id
            return res;
        }
    });
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • side note: using `o1.id - o2.id` exposes you to a risk of overflow, with unpredicted results. – njzk2 Jun 15 '15 at 22:02
0

With Java8:

    taskdet.sort((a, b) -> {
        int r = a.date - b.date;
        return r == 0 ? r : a.id - b.id;
    });