Statistics

Problem Statement for "NameSort"

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.

Definition

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

Constraints

  • 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

Examples

  1. {"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.

  2. {"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.

  3. {"Trudy","Trudy","TRUDY"}

    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.

  4. {"a b c d","A","Thomas a","d d", "ed edgars","al Adams"}

    Returns: { "Thomas a", "A", "al Adams", "d d", "a b c d", "ed edgars" }

  5. {"TurnipHeadJohnson"}

    Returns: { "TurnipHeadJohnson" }

  6. {"a a a a a a a a a a","A A A A A A A A A A","bob"}

    Returns: { "A A A A A A A A A A", "a a a a a a a a a a", "bob" }

  7. {"b","a","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X"}

    Returns: { "A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "y", "z" }

  8. {"bB","aA","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","MM","N","O","P","QQ","R","S S","T","U","V","W","X"}

    Returns: { "A", "aA", "B", "bB", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "m", "MM", "N", "n", "O", "o", "P", "p", "q", "QQ", "R", "r", "S S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "y", "z" }

  9. {"tIm JoNeS", "Tim Jones", "tom JoNes", "tim joness", "tiM joneS"}

    Returns: { "tiM joneS", "tom JoNes", "Tim Jones", "tIm JoNeS", "tim joness" }

  10. {"b b b","b bb b","b bb bb","B BB BB","b bb bb"}

    Returns: { "b bb b", "b b b", "b bb bb", "B BB BB", "b bb bb" }

  11. {"Bob Mackey","Mack Bobby", "bobby","Mack Mackey","Mackey"}

    Returns: { "bobby", "Mack Bobby", "Mackey", "Mack Mackey", "Bob Mackey" }

  12. {"Alan","aLan","alAn","alaN","ALan","AlAn","AlaN","aLAn","aLaN","alAN"}

    Returns: { "alAN", "aLaN", "aLAn", "AlaN", "AlAn", "ALan", "alaN", "alAn", "aLan", "Alan" }

  13. {"Doug B Doug", "b Doug doug", "bb Doug", "Doug b b", "DoUg BB"}

    Returns: { "Doug b b", "DoUg BB", "bb Doug", "b Doug doug", "Doug B Doug" }

  14. {"abcdefghijklmnop T", "abccefghijklmnop T", "t tt ttt tttt", "TTTT TTT TT T"}

    Returns: { "TTTT TTT TT T", "abccefghijklmnop T", "abcdefghijklmnop T", "t tt ttt tttt" }

  15. {"Al Thompson","Bob Johnson", "John Thompson", "John D Thompson","Bob D Johnson"}

    Returns: { "Bob D Johnson", "Bob Johnson", "John D Thompson", "John Thompson", "Al Thompson" }

  16. { "Emily Halliburton", "Madonna" }

    Returns: { "Emily Halliburton", "Madonna" }

  17. { "Alan", "aLan", "alAn", "alaN", "ALan", "AlAn", "AlaN", "aLAn", "aLaN", "alAN" }

    Returns: { "alAN", "aLaN", "aLAn", "AlaN", "AlAn", "ALan", "alaN", "alAn", "aLan", "Alan" }

  18. { "zZzZz", "A", "zZzZZ", "b", "zZZzzZ", "zzzzzz", "zZZzzZ", "c", "ZZZZZZ", "zzZZzz", "d" }

    Returns: { "A", "b", "c", "d", "zZzZZ", "zZzZz", "zzZZzz", "ZZZZZZ", "zZZzzZ", "zzzzzz", "zZZzzZ" }

  19. { "aa", "bb", "jj", "qq", "jj", "zz", "aa", "jj" }

    Returns: { "aa", "aa", "bb", "jj", "jj", "jj", "qq", "zz" }

  20. { "X", "x", "X" }

    Returns: { "X", "x", "X" }

  21. { "Alan", "aLan", "alAn", "alaN", "ALan", "AlAn", "AlaN", "aLan", "aLaN", "alAN" }

    Returns: { "alAN", "aLaN", "aLan", "AlaN", "AlAn", "ALan", "alaN", "alAn", "aLan", "Alan" }

  22. { "A", "a", "b", "B", "B A C", "A B C" }

    Returns: { "a", "A", "B", "b", "A B C", "B A C" }

  23. { "d a", "c e", "a b", "b b", "c b", "d b", "a a", "a c", "d c" }

    Returns: { "a a", "d a", "d b", "c b", "b b", "a b", "d c", "a c", "c e" }

  24. { "plz work", "break it plz", "this is first", "this is second", "this is plz", "ta", "za", "real first", "not second", "this is PLZ", "PLZ PLZ", "work work", "wrok work", "plz plz", "A B F work", "work", "plz", "a a", "b", "stablesort maybe" }

    Returns: { "a a", "b", "real first", "this is first", "stablesort maybe", "plz", "plz plz", "PLZ PLZ", "this is PLZ", "this is plz", "break it plz", "not second", "this is second", "ta", "work", "A B F work", "wrok work", "work work", "plz work", "za" }

  25. { "k a", "m a", "k a" }

    Returns: { "k a", "m a", "k a" }

  26. { "AAaaaa", "aAaaAa", "aaaAAA", "aaaAaA", "AAaaaA", "AaAAAa", "aAaAAa", "aAAAaA", "AaaaAa", "AAAaAa", "aaaAaa", "AAAAaa", "AaaaaA", "AaAAaa", "aAAAAA", "aAAaAa", "AAaaAa", "aAAaAA", "AaaaAA", "aaaaaa", "aaaaaA", "aAAAaa", "aAAAAa", "aaAaAa", "aaAaaa", "AAAaaa", "aaAaaA", "aaAAAa", "aAAaaa", "aaaaAA", "AAAAAa", "AAAAaA", "AAaaAA", "AaAaAA", "aAaaaa", "aAaAAA", "AaaAAa", "aaaaAa", "aaAAaa", "AaaAaA", "AaAaaA", "aAaaaA", "AaAaAa", "aaaAAa", "AAaAAa", "aAaAaA", "aAaAaa", "aAaaAA", "AAaAaa", "AaAAaA" }

    Returns: { "AaAAaA", "AAaAaa", "aAaaAA", "aAaAaa", "aAaAaA", "AAaAAa", "aaaAAa", "AaAaAa", "aAaaaA", "AaAaaA", "AaaAaA", "aaAAaa", "aaaaAa", "AaaAAa", "aAaAAA", "aAaaaa", "AaAaAA", "AAaaAA", "AAAAaA", "AAAAAa", "aaaaAA", "aAAaaa", "aaAAAa", "aaAaaA", "AAAaaa", "aaAaaa", "aaAaAa", "aAAAAa", "aAAAaa", "aaaaaA", "aaaaaa", "AaaaAA", "aAAaAA", "AAaaAa", "aAAaAa", "aAAAAA", "AaAAaa", "AaaaaA", "AAAAaa", "aaaAaa", "AAAaAa", "AaaaAa", "aAAAaA", "aAaAAa", "AaAAAa", "AAaaaA", "aaaAaA", "aaaAAA", "aAaaAa", "AAaaaa" }

  27. { "tIm JoNeS", "Tim Jones", "tom JoNes", "tim joness", "tiM joneS" }

    Returns: { "tiM joneS", "tom JoNes", "Tim Jones", "tIm JoNeS", "tim joness" }

  28. { "Kenny G", "Kenny Gee", "A DeForest Hoar", "C A R Hoare" }

    Returns: { "Kenny G", "Kenny Gee", "A DeForest Hoar", "C A R Hoare" }

  29. { "C A", "B B", "A B" }

    Returns: { "C A", "A B", "B B" }


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: