Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W7][M11-1]Lee Chong Wei Justin #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ Views all details of the 1st person in the results of the `find` command.
Clears all entries from the address book. +
Format: `clear`

== Sorting all entries : `sort`

Sorts all entries in the address book by name in lexicographical order. +
Format: `sort`

== Exiting the program : `exit`

Exits the program. +
Expand Down
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seedu.addressbook.commands;

/**
* Sorts all persons in the address book by name in lexicographical order.
*/
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Sorts all persons in the address book by name in lexicographical order.\n"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Address book sorted.";

@Override
public CommandResult execute() {
addressBook.sort();
return new CommandResult(MESSAGE_SUCCESS);
}
}
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public void clear() {
allPersons.clear();
}

/**
* Sorts all persons in the address book by name in lexicographical order.
*/
public void sort() {
allPersons.sort();
}

/**
* Defensively copied UniquePersonList of all persons in the address book at the time of the call.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/seedu/addressbook/data/person/ComparePerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package seedu.addressbook.data.person;

import java.util.Comparator;

/**
* Compares person by name
*/
public class ComparePerson implements Comparator<Person> {

@Override
public int compare(Person person1, Person person2){
return person1.getName().fullName.compareToIgnoreCase(person2.getName().fullName);
}
}
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public void clear() {
internalList.clear();
}

/**
* Sorts all persons in list.
*/
public void sort() {
internalList.sort(new ComparePerson());
}

@Override
public Iterator<Person> iterator() {
return internalList.iterator();
Expand Down
3 changes: 3 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public Command parseCommand(String userInput) {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case SortCommand.COMMAND_WORD:
return new SortCommand();

case ViewCommand.COMMAND_WORD:
return prepareView(arguments);

Expand Down