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

S Pratap Singh kdarious at gmail.com
Tue Apr 24 00:06:46 CEST 2012


One more this I did not add.... This sorting needs to be done on the last
name(Initial) of the user if the user does not have last name (initial)
then the first name is considered as initial.

On Tue, Apr 24, 2012 at 1:13 AM, S Pratap Singh <kdarious at gmail.com> wrote:

> 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
>
>


-- 
Regards,
Pratap Singh


More information about the BangPypers mailing list