# [Numpy-discussion] array sort questions

Tim Hochberg tim.hochberg at ieee.org
Thu Sep 18 11:35:10 CDT 2003

```Hi Chunlei,

I just realized one other thing that you should probably be aware of.
You could write a much faster version of sortrank in pure python by
doing your sorts differently. Python's built in sort is very fast, but
as soon as you start passing in comparison functions it slows down
dramatically. The trick is to arange the data you need to sort so that
you don't need an auxilliary function  (know asDecorate-Sort-Undecorate
or the Schwartzian transform). Thus, the following is almost certainly a
lot faster than your original sortrank, although probably still slower
than the argsort solution.

def sortrank(list):
index = range(len(list))
li_a = zip(list, index)
li_a.sort()
li_b = [(li_a[i][1], i) for i in index]
li_b.sort()
return [x[1] for x in li_b]

Regards,

-tim

CL WU wrote:

>>> I hope there is an efficient function in array level to do the same
>>> work.
>>> >>> from Numeric import *
>>> >>> a=array([5,2,3])
>>> >>> argsort(a)
>>> array([1, 2, 0])
>>> >>> def sortrank(list):
>>> ...     n=len(list)
>>> ...     li_a=[(i,list[i]) for i in range(n)]
>>> ...     li_a.sort(lambda a,b:cmp(a[1],b[1]))
>>> ...     li_b=[(i,li_a[i]) for i in range(n)]
>>> ...     li_b.sort(lambda a,b:cmp(a[1][0],b[1][0]))
>>> ...     return [x[0] for x in li_b]
>>> ...    >>> sortrank(a)
>>> [2, 0, 1]
>>> >>> def sortrank2(li):
>>> ...     li_sorted=li[:]
>>> ...     li_sorted.sort()
>>> ...     return [li_sorted.index(x) for x in li]
>>> >>> sortrank1(list(a))
>>> [2, 0, 1]
>>>
>>>
>>> Thanks again.
>>>
>>> Chunlei
>>>
>>> Tim Hochberg wrote:
>>>
>>>> CL WU wrote:
>>>>
>>>>> Hi, group,
>>>>>          I am new to numpy. I have 2 questions for array sort.
>>>>>
>>>>> 1. How to sort an array by its one column or one row?
>>>>>    I know python build-in sort() can do it for list by passing own
>>>>> cmp function. but array function sort() will sort each column or
>>>>> row seperately,as I know. I don't want to convert array to list to
>>>>> sort and then convert back to array.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> I think you want argsort plus take. For example, the following
>>>> sorts on the second column of a:
>>>>
>>>>     a = array([[4,5,6], [1,2,3], [7,8,9]])
>>>>     arg = argsort(a[:,1])
>>>>     take(a, arg, 0)
>>>>
>>>>> 2. How to get the rank of a rank-0 array? The first "rank" means
>>>>> the order of each element after sorting, instead of the
>>>>> "dimension" meaning in numpy.     Just like "rank()" function in
>>>>> splus.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> If  I understand you correctly, you want argsort as mentioned above.
>>>>
>>>> Regards,
>>>>
>>>> -tim
>>>>
>>>>
>>>>>
>>>>> Thank you
>>>>>
>>>>> Chunlei
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> -------------------------------------------------------
>>>>> This sf.net email is sponsored by:ThinkGeek
>>>>> Welcome to geek heaven.
>>>>> http://thinkgeek.com/sf
>>>>> _______________________________________________
>>>>> Numpy-discussion mailing list
>>>>> Numpy-discussion at lists.sourceforge.net
>>>>> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> -------------------------------------------------------
>>>> This sf.net email is sponsored by:ThinkGeek
>>>> Welcome to geek heaven.
>>>> http://thinkgeek.com/sf
>>>> _______________________________________________
>>>> Numpy-discussion mailing list
>>>> Numpy-discussion at lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>>>>
>>>
>>>
>>>
>>>
>>> -------------------------------------------------------
>>> This sf.net email is sponsored by:ThinkGeek
>>> Welcome to geek heaven.
>>> http://thinkgeek.com/sf
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>>>
>>
>>
>>
>>
>
>
>

```