forked from nus-cs2103-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2103-AY2021S1#108 from chrystalquek/autosort…
…-person-list Autosort person list
- Loading branch information
Showing
7 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/seedu/address/model/person/util/PersonComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package seedu.address.model.person.util; | ||
|
||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.Comparator; | ||
|
||
import seedu.address.model.person.person.Name; | ||
import seedu.address.model.person.person.Person; | ||
|
||
/** | ||
* Represents a Comparator for Person which sorts by Name. | ||
*/ | ||
public class PersonComparator implements Comparator<Person> { | ||
/** | ||
* Compares Persons by Name in ascending order. | ||
*/ | ||
public int compare(Person a, Person b) { | ||
requireAllNonNull(a, b); | ||
Name aName = a.getName(); | ||
Name bName = b.getName(); | ||
|
||
return aName.compareTo(bName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/seedu/address/model/person/util/PersonComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package seedu.address.model.person.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.testutil.Assert.assertThrows; | ||
import static seedu.address.testutil.TypicalPersons.ALICE; | ||
import static seedu.address.testutil.TypicalPersons.BENSON; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.person.person.Person; | ||
import seedu.address.testutil.PersonBuilder; | ||
|
||
public class PersonComparatorTest { | ||
|
||
private final PersonComparator personComparator = new PersonComparator(); | ||
|
||
@Test | ||
public void isCorrectOrder() { | ||
assertTrue(personComparator.compare(ALICE, ALICE) == 0); | ||
|
||
assertTrue(personComparator.compare(ALICE, BENSON) < 0); | ||
assertTrue(personComparator.compare(BENSON, ALICE) > 0); | ||
|
||
// Alice Pauline < alice Pauline | ||
Person editedAlice = new PersonBuilder(ALICE).withName("alice Pauline").build(); | ||
assertTrue(personComparator.compare(ALICE, editedAlice) < 0); | ||
|
||
// Alice Pauline > Alice | ||
editedAlice = new PersonBuilder(ALICE).withName("Alice").build(); | ||
assertTrue(personComparator.compare(ALICE, editedAlice) > 0); | ||
|
||
// null Persons | ||
assertThrows(NullPointerException.class, () -> personComparator.compare(null, ALICE)); | ||
assertThrows(NullPointerException.class, () -> personComparator.compare(ALICE, null)); | ||
} | ||
} |