[Numpy-discussion] 1D array sorting ascending and descending by fields
ben.root at ou.edu
Tue Jun 5 12:43:28 EDT 2012
On Tue, Jun 5, 2012 at 10:49 AM, Nathaniel Smith <njs at pobox.com> wrote:
> On Tue, Jun 5, 2012 at 1:17 AM, Benjamin Root <ben.root at ou.edu> wrote:
> > On Monday, June 4, 2012, Chris Barker wrote:
> >> On Mon, Jun 4, 2012 at 11:10 AM, Patrick Redmond <plredmond at gmail.com>
> >> wrote:
> >> > Here's how I sorted primarily by field 'a' descending and secondarily
> >> > field 'b' ascending:
> >> could you multiply the numeric field by -1, sort, then put it back --
> >> somethign like:
> >> data *- -1
> >> data_sorted = np.sort(data, order=['a','b'])
> >> data_sorted *= -1
> >> (reverse if necessary -- I lost track...)
> >> -Chris
> > While that may work for this users case, that would not work for all
> > Some, such as timedelta, datetime and strings would not be able to be
> > multiplied by a number.
> > Would be an interesting feature to add, but I am not certain if the
> > sign notation would be best. Is it possible for a named field to start
> > a negative sign?
> Maybe add a reverse= argument (named after the corresponding argument
> to list.sort and __builtins__.sorted).
> # sorts in descending order, no fields required
> np.sort([10, 20, 0], reverse=True)
> # sorts in descending order
> np.sort(rec_array, order=("a", "b"), reverse=True)
> # ascending by "a" then descending by "b"
> np.sort(rec_array, order=("a", "b"), reverse=(False, True))
Clear, unambiguous, and works with the existing framework.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion