SpreadTooThin 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
>
> Read that top to bottom from column 1 to column 4.
> When you get to the bottom of a column move to the next column.
> Get it?
It's a little vague, but i'm supposing that if you have an 11 in a the
order will be:
[1,4,7,10, 2,5,8,11, 3, 6,9]
If this holds then your order is based on x%3. You place first all x
for which x%3 == 1, then x%3 == 2, and last x%3 == 0. And among these
three group you use "natural" order.
 so:
def yourcmp(i,j):
   ri = i%3
   rj = j%3
   if ri == rj: return i.__cmp__(j)
   elif ri == 0: return 1
   elif rj == 0: return -1
   else: return ri.__cmp__(rj)
This works with your example, and with my assumption, feel free to
"optimize" the if/elif block.

However you shuold pay attention to the 0 behavior.
a = range(11)
a.sort(yourcmp)
print a
[1, 4, 7, 10, 2, 5, 8, 0, 3, 6, 9]

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to