TopCoder problem "NameSort" used in TCCC '03 NE/SE Regional (Division I Level One)

Problem Statement

    A certain business maintains a list of all its customers' names. The list is arranged in order of importance, with the last customer in the list being the most important. We want to create a new list sorted alphabetically according to customers' last names, but among customers with the same last name we want the more important ones to appear earlier in the new list. Alphabetical order (and equality of last names) should not be case sensitive.

Create a class NameSort that contains a method newList that takes a String[] list of names as input and returns the new sorted list as a String[]. The last name of a customer is defined to be the part of the name following the last space character, or the whole name if it has no space characters. The last name of "Bob E Jones" is "Jones". The last name of "Madona" is "Madona".

The names in the new sorted list should retain the same capitalization as they had in the original list.



Method signature:String[] newList(String[] list)
(be sure your method is public)


-list will contain between 1 and 50 elements, inclusive
-each element of list will contain between 1 and 20 characters, inclusive
-each character in each element of list will be a space, ' ', or a letter ('A'-'Z' or 'a'-'z')
-no element of list will contain leading or trailing spaces
-no element of list will contain two or more adjacent spaces


{"Tom Jones","ADAMS","BOB ADAMS",
"Tom Jones","STEVE jONeS"}
Returns: { "BOB ADAMS",  "ADAMS",  "STEVE jONeS",  "Tom Jones",  "Tom Jones" }
ADAMS comes before JONES. The ADAMS names are listed in reverse order as compared to the original list. Likewise for the JONES names.
{"C A R Hoare","Kenny G",
"A DeForest Hoar","Kenny Gee"}
Returns: { "Kenny G",  "Kenny Gee",  "A DeForest Hoar",  "C A R Hoare" }
No two of these names have the same last name. So the final list is the case-insensitive alphabetically ordered by last name version of the original.
Returns: { "TRUDY",  "Trudy",  "Trudy" }
All three have the same last name. So they are sorted by importance, which corresponds to the reverse order as compared with the original list.
{"tIm JoNeS", "Tim Jones", "tom JoNes", "tim joness", "tiM joneS"}
Returns: { "tiM joneS",  "tom JoNes",  "Tim Jones",  "tIm JoNeS",  "tim joness" }
{ "alAN",
 "Alan" }
{"Al Thompson","Bob Johnson", "John Thompson", "John D Thompson","Bob D Johnson"}
{ "Bob D Johnson",
 "Bob Johnson",
 "John D Thompson",
 "John Thompson",
 "Al Thompson" }

Problem url:

Problem stats url:




lbackstrom , brett1479

Problem categories:

Sorting, String Parsing