diff --git a/src/main/java/seedu/trippie/command/AddPlaceCommand.java b/src/main/java/seedu/trippie/command/AddPlaceCommand.java index 40ef15183a..a676c0093e 100644 --- a/src/main/java/seedu/trippie/command/AddPlaceCommand.java +++ b/src/main/java/seedu/trippie/command/AddPlaceCommand.java @@ -105,15 +105,21 @@ public void execute(Ui ui, TrippieData trippieData) { } public void sortPlaceList(List sortedPlaces) { - for (int i = 0; i < sortedPlaces.size(); i++) { - for (int j = i + 1; j < sortedPlaces.size(); j++) { + Boolean swapped = false; + for (int i = (sortedPlaces.size() - 1); i >= 0; i--) { + for (int j = i - 1; j >= 0; j--) { if (sortedPlaces.get(i).getPlaceDay() == sortedPlaces.get(j).getPlaceDay() - && sortedPlaces.get(i).getPlaceStartTime() > sortedPlaces.get(j).getPlaceStartTime()) { + && sortedPlaces.get(i).getPlaceStartTime() < sortedPlaces.get(j).getPlaceStartTime()) { Collections.swap(sortedPlaces, i, j); - } else if (sortedPlaces.get(i).getPlaceDay() > sortedPlaces.get(j).getPlaceDay()) { + swapped = true; + } else if (sortedPlaces.get(i).getPlaceDay() < sortedPlaces.get(j).getPlaceDay()) { Collections.swap(sortedPlaces, i, j); + swapped = true; } } + if (swapped == false) { + break; + } } } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 6b54cf4157..4e1b3222d5 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -63,8 +63,8 @@ _________________________________________________________________________ DAY 1: (2023-01-01) [1] 0200 - 0400 clementi mall DAY 3: (2023-01-03) -[2] 1200 - 1400 nus and ntu -[3] 1200 - 1400 Apple MBS +[2] 1200 - 1400 Apple MBS +[3] 1200 - 1400 nus and ntu _________________________________________________________________________ >> _________________________________________________________________________ Noted. I've removed this place from the place list. @@ -73,8 +73,8 @@ Now you have 2 places in the list. _________________________________________________________________________ >> _________________________________________________________________________ DAY 3: (2023-01-03) -[1] 1200 - 1400 nus and ntu -[2] 1200 - 1400 Apple MBS +[1] 1200 - 1400 Apple MBS +[2] 1200 - 1400 nus and ntu _________________________________________________________________________ >> _________________________________________________________________________ Got it. I've added this place: @@ -91,8 +91,8 @@ DAY 2: (2023-01-02) [1] 0700 - 0800 ntu [2] 1000 - 1200 clementi DAY 3: (2023-01-03) -[3] 1200 - 1400 nus and ntu -[4] 1200 - 1400 Apple MBS +[3] 1200 - 1400 Apple MBS +[4] 1200 - 1400 nus and ntu _________________________________________________________________________ >> _________________________________________________________________________ Got it. I've added this place: @@ -104,8 +104,8 @@ DAY 2: (2023-01-02) [1] 0700 - 0800 ntu [2] 1000 - 1200 clementi DAY 3: (2023-01-03) -[3] 1200 - 1400 nus and ntu -[4] 1200 - 1400 Apple MBS +[3] 1200 - 1400 Apple MBS +[4] 1200 - 1400 nus and ntu DAY 5: (2023-01-05) [5] 1200 - 1400 nus Soc _________________________________________________________________________