diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 7e4858342..7098fd5d8 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -54,6 +54,11 @@ Examples: Shows a list of all persons in the address book. + Format: `list` +== Listing all persons in sorted order : `sort` + +Shows a list of all persons sorted in alphabetical order in the address book. + +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..541a3536a 100644 --- a/src/seedu/addressbook/commands/Command.java +++ b/src/seedu/addressbook/commands/Command.java @@ -36,6 +36,10 @@ public static String getMessageForPersonListShownSummary(List personsDisplayed) { + return String.format(Messages.MESSAGE_PERSON_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..4cb24a47e --- /dev/null +++ b/src/seedu/addressbook/commands/SortCommand.java @@ -0,0 +1,31 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.data.AddressBook; + +import seedu.addressbook.data.person.ReadOnlyPerson; + +import java.util.*; +import java.lang.*; + + +/** + * Lists all persons in the address book in sorted order to the user. + */ +public class SortCommand extends Command { + + public static final String COMMAND_WORD = "sort"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n" + + "Displays all persons in the address book as a list sorted in alphabetical order with index numbers.\n\t" + + "Example: " + COMMAND_WORD; + + + @Override + public CommandResult execute() { + addressBook.sorted(); + + List allPersons = addressBook.getAllPersons().immutableListView(); + return new CommandResult(getMessageForPersonSortShownSummary(allPersons), allPersons); + + } +} diff --git a/src/seedu/addressbook/common/Messages.java b/src/seedu/addressbook/common/Messages.java index 02cfe6155..65e9318fc 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_PERSON_SORTED_OVERVIEW = "%1$d persons sorted in alphabetical order!"; 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..1c6322f63 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -58,6 +58,12 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException allPersons.remove(toRemove); } + /** + * Sorts all persons from the address book. + */ + + public void sorted() { allPersons.sort();} + /** * Clears all persons from the address book. */ diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index 487b7ad9c..a74f6c337 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -9,7 +9,7 @@ * Represents a Person's name in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidName(String)} */ -public class Name { +public class Name implements Comparable{ public static final String EXAMPLE = "John Doe"; public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphanumeric characters"; @@ -61,4 +61,13 @@ public int hashCode() { return fullName.hashCode(); } + + /**compare name for sorting function*/ + @Override + public int compareTo(Name name) { + return this.fullName.compareTo(name.fullName); + } + + + } diff --git a/src/seedu/addressbook/data/person/UniquePersonList.java b/src/seedu/addressbook/data/person/UniquePersonList.java index c4848a1b4..533c058f7 100644 --- a/src/seedu/addressbook/data/person/UniquePersonList.java +++ b/src/seedu/addressbook/data/person/UniquePersonList.java @@ -106,6 +106,8 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException { } } + + /** * Clears all persons in list. */ @@ -113,6 +115,18 @@ public void clear() { internalList.clear(); } + /** + * Sorts all persons in list by name. + */ + public void sort(){ + Comparator person = (p1, p2) -> { + Name name1 = p1.getName(); + Name name2 = p2.getName(); + return name1.compareTo(name2); + }; + Collections.sort(internalList, person); + } + @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..cc42595e6 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -78,10 +78,14 @@ public Command parseCommand(String userInput) { case ViewAllCommand.COMMAND_WORD: return prepareViewAll(arguments); + case SortCommand.COMMAND_WORD: + return new SortCommand(); + + case ExitCommand.COMMAND_WORD: return new ExitCommand(); - case HelpCommand.COMMAND_WORD: // Fallthrough + case HelpCommand.COMMAND_WORD:// Fallthrough default: return new HelpCommand(); } diff --git a/test/java/seedu/addressbook/parser/ParserTest.java b/test/java/seedu/addressbook/parser/ParserTest.java index 5b5f5b013..cc1fb554d 100644 --- a/test/java/seedu/addressbook/parser/ParserTest.java +++ b/test/java/seedu/addressbook/parser/ParserTest.java @@ -58,6 +58,12 @@ public void listCommand_parsedCorrectly() { parseAndAssertCommandType(input, ListCommand.class); } + @Test + public void sortCommand_parsedCorrectly() { + final String input = "sort"; + parseAndAssertCommandType(input, SortCommand.class); + } + @Test public void exitCommand_parsedCorrectly() { final String input = "exit";