Need a strange sort method...

Simon Brunning simon at brunningonline.net
Mon Oct 16 20:30:45 CEST 2006


On 16 Oct 2006 11:13:08 -0700, SpreadTooThin <bjobrien62 at gmail.com> wrote:
> I have a list and I need to do a custom sort on it...
>
> for example:
> a = [1,2,3,4,5,6,7,8,9,10] #Although not necessarily in order
>
> def cmp(i,j):  #to be defined in this thread.
>
> a.sort(cmp)
>
> print a
> [1,4,7,10,  2,5,8, 3,6,9]
>
> So withouth making this into an IQ test.
> Its more like
> 1 4 7 10
> 2 5 8
> 3 6 9

>>> a = [1,2,3,4,5,6,7,8,9,10]
>>> a.sort(key=lambda item: (((item-1) %3), item))
>>> a
[1, 4, 7, 10, 2, 5, 8, 3, 6, 9]

-- 
Cheers,
Simon B
simon at brunningonline.net
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list