Sort names in a list by order of importance

Hi,

I have multiple list which is arranged by order of importance. I need to do sorting on these lists based on the last name of the user(initial), if user name does not have initial then first name is initial . Important thing is that the last name in the list is important. If there is two or more similar name as the last name in the list then the last name should be printed first. Also I need to make sure that alphabets case do not change after sorting.

def mycmp(a, b):

    if a.startswith(b):

        return -1

    return cmp(a, b)


mylist = ["Ramesh G","K V R Santhosh","Srikanth T G", "R V Laxaman","Ramesh Ghosh"]

mylist.sort(cmp=mycmp)

print "\n".join(mylist)

Output:

K V R Santhosh

R V Laxaman

Ramesh Ghosh

Ramesh G

Srikanth T G

Above works fine but in the scenario mentioned below it fails :

>>> mylist = ["Alan","aLan","alAn","alaN","ALan","AlAn","AlaN","aLAn","aLaN","alAN"] 
>>> mylist.sort(cmp=mycmp) 
>>> print "\n".join(mylist) 
ALan AlAn AlaN Alan aLAn aLaN aLan alAN alAn alaN 

The desired output should be like this :

[ "alAN", "aLaN", "aLAn", "AlaN", "AlAn", "ALan", "alaN", "alAn", "aLan", "Alan" ]

Any clue how can we do this?

Append the ordinal position before sorting and remove it after.

eg sort this:

["Alan_1","aLan_2","alAn_3","alaN_4","ALan_5","AlAn_6","AlaN_7","aLAn_8","aLaN_9","alAN_10"] 

And strip the "_[0-9]*$" after the sort is done