I'm trying a way that when given a string of names which are first and last names, where names are split by ; and first name split to last name by : ->
"Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"
I want to return a string which is sorted out all the names as uppercase and sorted alphabetically according to the last name and the ones that share the same last name then sort again (secondary sort) between the first names of the people that share the same last name. (I also on purpose change everything to UPPERCASE).
And return a string of the sorted names, so for the example above it should return:
"(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)"
which sorts according to the last name alphabetically then sorts between those who share the same last name a secondary sort of the first names.
I managed to do the main sorting which is sorting the last names alphabetically but I'm not sure how to do the secondary sorting (between those who share the same last name). I was thinking to split into sub-arrays or something similar but I'm not sure how to do this.
What is the solution for this?
Here is what I managed so far (works on main sorting):
public class Solution {
public static String meeting(String s) {
String m = "";
List<String> name = new ArrayList<String>();
for (String i : s.toUpperCase().split(";")) {
String[] n = i.split(":");
name.add(n[0] + " " + n[1]);
}
//main sorting
java.util.Collections.sort(name);
//secondary sorting and go through each different last name
for (String i : name) {
String[] n = i.split(" ");
String lastname = n[1];
// new list for the ppl who share same last name
List<String> samel = new ArrayList<String>();
samel.add(n[0]);
for (String ss : name) {
String[] nn = ss.split(" ");
if (nn[1] == lastname) {
samel.add(nn[0]);
}
}
//sort the ppl alphabetically with the same last name
java.util.Collections.sort(samel);
}
for (String i : name) {
String[] n = i.split(" ");
m += ("(" + n[0] + " , " + n[1] + ")");
}
return m;
}
}
I attempted to do the secondary sorting and was not successful.
If I wasn't clear enough, there are two sortings, the main which is by last names alphabetically and the secondary sorting which only happens on people who share the exact same last name then their first names get sorted accordingly by alphabet. So, if we have two persons called matt cohn and mill cohn, then they share the same last name and the same first letter in the last name, but a is before o, so the output should be (COHN , MATT)(COHN, MILL) and not the other way round.
On the solution down below I ran it and got an error:
input String:
Alexis:Wahl;John:Bell;Victoria:Schwarz;Abba:Dorny;Grace:Meta;Ann:Arno;Madison:STAN;Alex:Cornwell;Lewis:Kern;Megan:Stan;Alex:Korn
Actual: (ARNO, ANN)(BELL, JOHN)(CORNWELL, ALEX)(DORNY, ABBA)(KERN, LEWIS)(KORN, ALEX)(META, GRACE)(STAN, MADISON)(SCHWARZ, VICTORIA)(STAN, MEGAN)(WAHL, ALEXIS)
Expect: (ARNO, ANN)(BELL, JOHN)(CORNWELL, ALEX)(DORNY, ABBA)(KERN, LEWIS)(KORN, ALEX)(META, GRACE)(SCHWARZ, VICTORIA)(STAN, MADISON)(STAN, MEGAN)(WAHL, ALEXIS)
false
but came out as false
Personwhich will store name, surname, and will provide getters for those, then create list of Person objects and fill it withPersonobjects likepeople.add(new Person(n[0], n[1]));). Then creating Comparator for sorting method would be easier. For instance you could write something likepeople.sort(Comparator.comparing(Person::getSurname).thenComparing(Person::getName));.nn[1]==lastname" usenn[1].equals(lastname)to compare strings. Also, unless you're using a version of Java prior to 8,list.sort()is simpler thanCollections.sort(list).