[Matrix-SIG] lexicographic sort and non-conservative argsort

Christian Cabos ca@germanlloyd.org
24 Sep 1998 19:24:38 -0000


I am trying to sort a Numeric-array lexicographically, i.e. sort by
values in first row. Then, if some values are equal in the first row, use
second row as secondary key. I think the following algorithm is fairly
standard (sort by secondary key first then by primary key), 

a=array([[2,2,0,2],
	 [1,2,3,4]])

# first sort by secondary key
ind2=argsort(a[1,:])
a=take(a,ind2,1)

# then sort by primary key (and hope that the order 
#                           of the secondary key is preserved)
ind1=argsort(a[0,:])
a=take(a,ind1,1)



but under Solaris it yields
[[0 2 2 2]
 [3 2 1 4]]
for 'a' which is wrong. Under Linux it yields
[[0 2 2 2]
 [3 1 2 4]]
which is correct. The reason is that argsort is not conservative 
under Solaris:

      argsort(array([2,2,0,2]))   is   [2 1 0 3]

instead of [2,0,1,3]. 

Is there any workaround for lexicogrphic sorting in Numeric except for
sorting something like a[0,:]*1000000+a[1,:] which is of very limited
use ?  

Can argsort be made conservative on all platforms in the future ?

Christian


Python 1.5 (#3, Jan 12 1998, 08:56:36) [GCC 2.7.2.2] on sunos5
Python 1.5 (#3, Apr 15 1998, 21:17:05)  [GCC 2.7.2.1] on linux2


-----
Free e-mail group hosting at http://www.eGroups.com/