diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 7e4858342..8cc5bb623 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -54,6 +54,10 @@ Examples: Shows a list of all persons in the address book. + Format: `list` +== Sorting all persons : `sort` +Sorts all persons in the address book based on their names lexicographically. + +Format: `sort` + == Finding all persons containing any keyword in their name: `find` Finds persons whose names contain any of the given keywords. + diff --git a/src/seedu/addressbook/commands/Command.java b/src/seedu/addressbook/commands/Command.java index a54cbcb5b..f31b10e22 100644 --- a/src/seedu/addressbook/commands/Command.java +++ b/src/seedu/addressbook/commands/Command.java @@ -36,6 +36,16 @@ public static String getMessageForPersonListShownSummary(List personsDisplayed) { + return String.format(Messages.MESSAGE_PERSONS_SORTED_OVERVIEW, personsDisplayed.size()); + } + /** * Executes the command and returns the result. */ diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index ef2ed7d0e..0c8006167 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -16,6 +16,7 @@ public class HelpCommand extends Command { + "\n" + ClearCommand.MESSAGE_USAGE + "\n" + FindCommand.MESSAGE_USAGE + "\n" + ListCommand.MESSAGE_USAGE + + "\n" + SortCommand.MESSAGE_USAGE + "\n" + ViewCommand.MESSAGE_USAGE + "\n" + ViewAllCommand.MESSAGE_USAGE + "\n" + HelpCommand.MESSAGE_USAGE diff --git a/src/seedu/addressbook/commands/SortCommand.java b/src/seedu/addressbook/commands/SortCommand.java new file mode 100644 index 000000000..eeb4b314e --- /dev/null +++ b/src/seedu/addressbook/commands/SortCommand.java @@ -0,0 +1,25 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.data.person.ReadOnlyPerson; + +import java.util.List; + + +/** + * Lists all persons in the address book to the user. + */ +public class SortCommand extends Command { + + public static final String COMMAND_WORD = "sort"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n" + + "Sorts all persons in the address book based on their names lexicographically.\n\t" + + "Example: " + COMMAND_WORD; + + + @Override + public CommandResult execute() { + List allPersons = addressBook.sortAllPersons().immutableListView(); + return new CommandResult(getMessageForSortedPersonListShownSummary(allPersons)); + } +} diff --git a/src/seedu/addressbook/common/Messages.java b/src/seedu/addressbook/common/Messages.java index 02cfe6155..6c77a664e 100644 --- a/src/seedu/addressbook/common/Messages.java +++ b/src/seedu/addressbook/common/Messages.java @@ -9,6 +9,7 @@ public class Messages { public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid"; public static final String MESSAGE_PERSON_NOT_IN_ADDRESSBOOK = "Person could not be found in address book"; public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; + public static final String MESSAGE_PERSONS_SORTED_OVERVIEW = "%1$d persons sorted!"; public static final String MESSAGE_PROGRAM_LAUNCH_ARGS_USAGE = "Launch command format: " + "java seedu.addressbook.Main [STORAGE_FILE_PATH]"; public static final String MESSAGE_WELCOME = "Welcome to your Address Book!"; diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index ba69c21e3..1c969ba3a 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -72,6 +72,11 @@ public UniquePersonList getAllPersons() { return new UniquePersonList(allPersons); } + public UniquePersonList sortAllPersons() { + allPersons.sort(); + return new UniquePersonList(allPersons); + } + @Override public boolean equals(Object other) { return other == this // short circuit if same object diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index 487b7ad9c..600feb046 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -44,6 +44,11 @@ public List getWordsInName() { return Arrays.asList(fullName.split("\\s+")); } + public int compareTo(Name target) { + int compare = this.fullName.compareTo(target.fullName); + return compare; + } + @Override public String toString() { return fullName; diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index fdd99358b..7dc72fd1a 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -3,6 +3,7 @@ import java.util.HashSet; import java.util.Objects; import java.util.Set; +import java.util.Comparator; import seedu.addressbook.data.tag.Tag; @@ -61,6 +62,15 @@ public Set getTags() { return new HashSet<>(tags); } + /** + * Compare selected person's name with target's name + */ + public static Comparator compareByName = new Comparator() { + public int compare(Person selected, Person target) { + return selected.name.compareTo(target.name); + } + }; + /** * Replaces this person's tags with the tags in {@code replacement}. */ diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index c4848a1b4..b32625003 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -113,6 +113,13 @@ public void clear() { internalList.clear(); } + /** + * Sorts the current list in lexicographocal order of names + */ + public void sort() { + Collections.sort(internalList, Person.compareByName); + } + @Override public Iterator iterator() { return internalList.iterator(); diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index 58f4f7e6c..2903c576a 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -72,6 +72,12 @@ public Command parseCommand(String userInput) { case ListCommand.COMMAND_WORD: return new ListCommand(); + case SortCommand.COMMAND_WORD: + if (arguments.isEmpty()) { + return new SortCommand(); + } + return new HelpCommand(); + case ViewCommand.COMMAND_WORD: return prepareView(arguments);