TopCoder problem "TheBestName" used in SRM 381 (Division II Level One)



Problem Statement

    

As some of you may know, there is no name better than JOHN. Let's define the rules for comparing names. Each letter has a weight ('A' - 1, 'B' - 2, ..., 'Z' - 26). The weight of a name is the sum of the weights of all its letters. For example, the name MARK has weight 13 + 1 + 18 + 11 = 43.

When comparing two names, the one with the larger weight is considered better. In case of a tie, the one that comes earlier lexicographically is better. But there is one exception - the name JOHN is the best name of all.

You are given a String[] names, each element of which contains a single name. Sort the names from best to worst and return the sorted String[].

 

Definition

    
Class:TheBestName
Method:sort
Parameters:String[]
Returns:String[]
Method signature:String[] sort(String[] names)
(be sure your method is public)
    
 

Constraints

-names will contain between 1 and 50 elements, inclusive.
-Each element of names will contain between 1 and 50 characters, inclusive.
-Each element of names will contain only uppercase letters ('A'-'Z').
 

Examples

0)
    
{"JOHN", "PETR", "ACRUSH"}
Returns: {"JOHN", "ACRUSH", "PETR" }
PETR has weight 59, ACRUSH has weight 70 and JOHN has a weight of only 47. But nevertheless JOHN is the best name, ACRUSH takes second place and PETR is the last.
1)
    
{"GLUK", "MARGARITKA"}
Returns: {"MARGARITKA", "GLUK" }
MARGARITKA is definitely better than GLUK.
2)
    
{"JOHN", "A", "AA", "AAA", "JOHN", "B", "BB", "BBB", "JOHN", "C", "CC", "CCC", "JOHN"}
Returns: 
{"JOHN",
"JOHN",
"JOHN",
"JOHN",
"CCC",
"BBB",
"CC",
"BB",
"AAA",
"C",
"AA",
"B",
"A" }
AA and B both have the same weight, but AA is better as it comes earlier lexicographically. For the same reason, AAA is better than C and BBB is better than CC.
3)
    
{"BATMAN", "SUPERMAN", "SPIDERMAN", "TERMINATOR"}
Returns: {"TERMINATOR", "SUPERMAN", "SPIDERMAN", "BATMAN" }
Here are some superheroes sorted by their names.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8413

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10804&pm=8413

Writer:

Vasyl[alphacom]

Testers:

PabloGilberto , Olexiy , ivan_metelsky , ged

Problem categories:

String Manipulation