[BangPypers] Sort names in a list by order of importance

S Pratap Singh kdarious at gmail.com
Mon Apr 23 21:43:44 CEST 2012


Hi,

I have multiple list which is arranged by order of importance. I need to do
sorting on this list. 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" ]


-- 
Regards,
Pratap Singh


More information about the BangPypers mailing list