Java, sorting ArrayList using comparator -



Java, sorting ArrayList using comparator -

i want sort list of task object 2 criterion. first sorted leftentrytime , timeneededtobeprocessed.

can help me?

so far did sorting leftentrytime how can sort timeneededtobeprocessed in same comparator class?

task(int procesid, int neededtime, int waitingtime, int leftentrytime) { this.procesid = procesid; this.timeneededtobeprocessed = neededtime; this.waitingtime = waitingtime; this.leftentrytime = leftentrytime; }

this comparator:

import java.util.comparator; public class timeentrycomparator implements comparator<task> { @override public int compare(task o1, task o2) { int entrytime; int tasktime = o1.getleftentrytime()- o2.getleftentrytime(); if (tasktime > 0){ homecoming 1; } if (tasktime < 0) { homecoming -1; } homecoming 0; } }

then sort by:

collections.sort(list, new timeentrycomparator());

i believe have do:

@override public int compare(task o1, task o2) { if (o1.getleftentrytime() == o2.getleftentrytime()) { homecoming o1.gettimeneededtobeprocessed - o2.gettimeneededtobeprocessed(); } else { homecoming o1.getleftentrytime() - o2.getleftentrytime(); } }

this compare task objects leftentrytime , if leftentrytime properties equal, compare timeneededtobeprocessed properties.

java sorting comparator

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -