473,890 Members | 1,513 Online

# Sorting a multidimensiona l array by multiple keys

Hello everyone,

can I sort a multidimensiona l array in Python by multiple sort keys? A
litte code sample would be nice!

Thx,
Rehceb

Mar 31 '07
16 3642
Peter Otten <__*******@web. dewrote:
Duncan Booth wrote:
>>>>from operator import itemgetter
data.sort(k ey=itemgetter(0 ))
data.sort(k ey=itemgetter(1 ))
data.sort(k ey=itemgetter(4 ))
data.sort(k ey=itemgetter(3 ))

Or, in Python 2.5:
>>>data.sort(ke y=itemgetter(3, 4, 1, 0))
Thanks, I'd forgotten itemgetter had that strangley assymmetric behaviour
of returning either a single value or a tuple.
Mar 31 '07 #11
Rehceb Rotkiv escreveu:
Hello everyone,

can I sort a multidimensiona l array in Python by multiple sort keys? A
litte code sample would be nice!
class MyList(list):
# This is the index of the element to be compared
CmpIndex=0

# Comparision methods
@staticmethod
def __cmp_pars(x,y) :
if isinstance(x,My List):
x=x[MyList.CmpIndex]
if isinstance(y,My List):
y=y[MyList.CmpIndex]
return x,y
def __cmp__(self,ot her):
s,o=MyList.__cm p_pars(self,oth er)
return cmp(s,o)
def __lt__(self,oth er):
s,o=MyList.__cm p_pars(self,oth er)
return s<o
def __le__(self,oth er):
s,o=MyList.__cm p_pars(self,oth er)
return s<=o
def __gt__(self,oth er):
s,o=MyList.__cm p_pars(self,oth er)
return s>o
def __ge__(self,oth er):
s,o=MyList.__cm p_pars(self,oth er)
return s>=o
def __eq__(self,oth er):
s,o=MyList.__cm p_pars(self,oth er)
return s==o
def __ne__(self,oth er):
s,o=MyList.__cm p_pars(self,oth er)
return s!=o

Use:

x=MyList(<list of lists>)
MyList.CmpIndex =2 # Compare by index 2
x.sort()

May be there is a better solution ...
HTH
Paulo
Apr 2 '07 #12
Thomas Krüger <ne********@nos pam.nowire.orgw rote:
Rehceb Rotkiv schrieb:
can I sort a multidimensiona l array in Python by multiple sort keys? A
litte code sample would be nice!

You can pass a function as argument to the sort method of a list.
The function should take two arguments and return -1, 0 or 1 as
comparison result. Just like the cmp function.

This will objects in list obj_lst by their id attributes:

def sorter(a, b):
return cmp(a.id, b.id)

obj_lst.sort(so rter)
A MUCH better way to obtain exactly the same semantics would be:

def getid(a):
return a.id

obj_list.sort(k ey=getid)
Alex
Apr 2 '07 #13
Alex Martelli schrieb:
Thomas Krüger <ne********@nos pam.nowire.orgw rote:
>def sorter(a, b):
return cmp(a.id, b.id)

obj_lst.sort(s orter)

A MUCH better way to obtain exactly the same semantics would be:

def getid(a):
return a.id

obj_list.sort(k ey=getid)
Frankly speaking the purpose of the example was to show how to pass a
function as argument for the sort method.
Your code may be more efficient but it explains something different.

Thomas
Apr 2 '07 #14
Thomas Krüger wrote:
Alex Martelli schrieb:
>Thomas Krüger <ne********@nos pam.nowire.orgw rote:
>>def sorter(a, b):
return cmp(a.id, b.id)

obj_lst.sort( sorter)
A MUCH better way to obtain exactly the same semantics would be:

def getid(a):
return a.id

obj_list.sort( key=getid)

Frankly speaking the purpose of the example was to show how to pass a
function as argument for the sort method.
Your code may be more efficient but it explains something different.
Yes, but there's almost never a reason to use the cmp= argument to
sort() anymore. It's almost always better to use the key= argument.

STeVe
Apr 2 '07 #15
Steven Bethard:
there's almost never a reason to use the cmp= argument to
sort() anymore. It's almost always better to use the key= argument.
I always use key now, but maybe cmp uses less memory. There can be few
situations where cmp is better still.

Bye,
bearophile

Apr 2 '07 #16
Steven Bethard <st************ @gmail.comwrote :
Thomas Krüger wrote:
Alex Martelli schrieb:
Thomas Krüger <ne********@nos pam.nowire.orgw rote:
def sorter(a, b):
return cmp(a.id, b.id)

obj_lst.sort(s orter)
A MUCH better way to obtain exactly the same semantics would be:

def getid(a):
return a.id

obj_list.sort(k ey=getid)
Frankly speaking the purpose of the example was to show how to pass a
function as argument for the sort method.
Your code may be more efficient but it explains something different.

Yes, but there's almost never a reason to use the cmp= argument to
sort() anymore. It's almost always better to use the key= argument.
Exactly. Passing a "comparison function" rather than a key-extraction
function is rarely a good idea -- except for some extremely complicated
cases involving e.g. certain string (or more complicated) fields needing
to be sorted in the reverse direction from others. In practice, the
existence of that argument and its prominent position as the first
positional argument is due entirely to backwards compatibility issues.
Alex
Apr 2 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.