Skip to content

Instantly share code, notes, and snippets.

@lucasmelin
Created April 25, 2016 16:20
Show Gist options
  • Save lucasmelin/40441fa8d69a0bce7265eb2aba5e8448 to your computer and use it in GitHub Desktop.
Save lucasmelin/40441fa8d69a0bce7265eb2aba5e8448 to your computer and use it in GitHub Desktop.
Sorting using completion comparator
/**
* Sorts the task list using the TaskIsCompleteComparator comparator.
* <p>
* Checks to make sure that the array is not empty before attempting to compare elements.
*/
private void sortByIsComplete(){
if (tasks.isEmpty()==false){
Collections.sort(tasks, new TaskIsCompleteComparator());
System.out.println("Sorting by Is-Complete is complete");
}
else{
System.out.print("Nothing to sort, no tasks");
}
}
import java.util.Comparator;
public class TaskIsCompleteComparator implements Comparator<Task>{
@Override
public int compare(Task t1, Task t2){
return Boolean.compare(t1.getIsComplete(), t2.getIsComplete());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment