2

This is not a duplicate of the question that is linked. Does this mean I have to open a new question?

I have a Task class

class Task {
String personId;
String taskId;
int sum;
}

I have a list of Tasks - List

1  task50   9   
1  task100  12
2  task50   10
3  task1    50

How do I do this?

Sort the list such that for taskid = task50, order by sum desc

Result list

2   task50   10
1   task50    9
1   task100  12
3   task1    50

To add a bit of clarity, result list should contain task50's at the top of the list sorted by sum in descending order. Rest of the list should appear as is in the original list.

jakew
  • 59
  • 1
  • 5
  • *Sort the list such that for taskid = task50, order by sum desc*. do you mean that only task with `taskid = task50` should be ordered, and by what do you sort? persionId or taskId or sum? – Lino Jul 26 '17 at 06:22
  • 2
    Tr this code Collections.sort(taskList, (t1, t2) -> { t1.taskId.compareTo(p2.taskId)); t1.sum > t2.sum; }; – Fady Saad Jul 26 '17 at 06:23
  • @Lino, I think he is trying to order by task.sum DESC just when task.taskId is task50 – Aldeguer Jul 26 '17 at 06:27
  • Yes, task50s should bubble up at the top, with descending order of sum for task50. Rest of the list should appear as is in the original list. – jakew Jul 26 '17 at 06:59
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. Because this is a duplicate regardless of what you think, the duplicate has everything you need to know to solve your problem! Asking the same question again, will just get you down voted and eventually question banned. You should really take the time to learn how to use the site properly. –  Jul 26 '17 at 16:38
  • Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) before attempting to ask more questions. –  Jul 26 '17 at 16:39

1 Answers1

1

Try this

taskList.sort((c1, c2) -> c1.getTaskId().equals("task50")&&c2.getTaskId().equals("task50")?c1.getSum().compareTo(c2.getSum()):null);
Aldeguer
  • 821
  • 9
  • 32
  • This throws a nullpointer – jakew Jul 26 '17 at 06:58
  • Well, that was a "blind" try since I have not access to an IDE so... it's possible. Using 0 instead of null should work since the comparator will understand they are equals. Give it a try: taskList.sort((c1, c2) -> c1.getTaskId().equals("task50")&&c2.getTaskId().equals("task50")?c1.getSum().compareTo(c2.getSum()):0); – Aldeguer Jul 26 '17 at 07:01
  • Nope, that doesn't work either – jakew Jul 26 '17 at 07:24
  • What's about using -1? haha I have just read your question again and, if you want to get taskId!=task50 the last ones, they are "lower than" so... try it – Aldeguer Jul 26 '17 at 08:12