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 Ying Rong #38

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 @@ -133,6 +133,11 @@ Views all details of the 2nd person in the address book.
`viewall 1` +
Views all details of the 1st person in the results of the `find` command.

== Sorting all entries : `sort`

Sort all entries from address book in ascending alphabetical order. +
Format : `sort`

== Clearing all entries : `clear`

Clears all entries from the address book. +
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 @@ -17,6 +17,7 @@ public class HelpCommand extends Command {
+ "\n" + FindCommand.MESSAGE_USAGE
+ "\n" + ListCommand.MESSAGE_USAGE
+ "\n" + ViewCommand.MESSAGE_USAGE
+ "\n" + SortCommand.MESSAGE_USAGE
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
+ "\n" + ExitCommand.MESSAGE_USAGE;
Expand Down
23 changes: 23 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.addressbook.commands;

/**
* Sorts 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 in ascending alphabetical order.\n\t"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Address book has been sorted!";

@Override
public CommandResult execute() {
addressBook.sort();
return new CommandResult(MESSAGE_SUCCESS);
}
}

5 changes: 5 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException
allPersons.remove(toRemove);
}

/**
* Sorts all persons from the address book.
*/
public void sort() { allPersons.sort(); }

/**
* Clears all persons from the address book.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Name> {

public static final String EXAMPLE = "John Doe";
public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphanumeric characters";
Expand Down Expand Up @@ -61,4 +61,8 @@ public int hashCode() {
return fullName.hashCode();
}

@Override
public int compareTo(Name name){
return this.fullName.compareToIgnoreCase(name.fullName);
}
}
9 changes: 9 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.addressbook.data.person;

import java.util.Comparator;
import seedu.addressbook.common.Utils;
import seedu.addressbook.data.exception.DuplicateDataException;

Expand Down Expand Up @@ -113,6 +114,14 @@ public void clear() {
internalList.clear();
}

/**
* Sort all persons in list by ascending alphabetical order.
*/
public void sort(){
Comparator<Person> CustomPersonCompare = Comparator.comparing(Person::getName);
Collections.sort(internalList,CustomPersonCompare);
}

@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