Multidimensional sort

Paul Hankin paul.hankin at gmail.com
Wed Oct 10 16:40:52 EDT 2007


On Oct 10, 8:47 pm, termiflyer <dpfl... at earthlink.net> wrote:
> How do I sort this:
>
> >>> a
>
> [['3', ['1', '0']], ['4', ['3', '0'], ['2', '0']]]
>
> where the list can be arbitrarily large by the 3rd dimension (i
> think).  E.g.:
>
> >>> a
>
> [['3', ['1', '0']], ['4', ['2', '0'], ['3', '0']]]

There's no canonical way to sort arbitrary data. For instance should
[1, 2, 3] come before or after [1, 3, 2]? Should [1] come before or
after [[1]]? Is there some structure to your data, or can it be any
values? Are all your strings made of digits, and should they be
compared as numbers (ie should '10' come after '2'?)

If you can say how to compare two of your data, say by defining a
compare function
def cmp(a, b):
   ...

Then you can sort your list using sorted(my_list, cmp=cmp).

Without knowing what your data means, and what you're planning to do
with it, it's not possible for anyone to answer your question
properly.

--
Paul Hankin




More information about the Python-list mailing list