Skip to content

Commit

Permalink
Add compareTo for Task
Browse files Browse the repository at this point in the history
  • Loading branch information
Skaty committed Nov 5, 2016
1 parent 73687c6 commit c98c812
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main/java/seedu/task/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Represents a Task in the task list.
* Guarantees: field values are validated.
*/
public class Task implements ReadOnlyTask {
public class Task implements ReadOnlyTask, Comparable<Task> {

private Name name;
private DateTime openTime;
Expand Down Expand Up @@ -149,4 +149,26 @@ public int hashCode() {
public String toString() {
return getAsText();
}

//@@author A0141052Y
/**
* Uses Comparator.compareTo.
* The comparison is based on:<br/>
* - Starred tasks have higher priority<br/>
* - Completed tasks have lower priority<br/>
* - For tasks with same completion/starred status,
* base comparison on endDate only
*/
@Override
public int compareTo(Task o) {
if ((this.isCompleted || o.getComplete()) && !(this.isCompleted && o.getComplete())) {
return (this.isCompleted) ? 1 : -1;
} else if ((this.isImportant || o.getImportance()) && !(this.isImportant && o.getImportance())) {
return (this.isImportant) ? -1 : 1;
} else if (!this.closeTime.isEmpty() && !o.getCloseTime().isEmpty()) {
return this.closeTime.compareTo(o.getCloseTime());
} else {
return 0;
}
}
}

0 comments on commit c98c812

Please sign in to comment.