summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/_internal.py18
-rw-r--r--numpy/core/src/arraymethods.c36
2 files changed, 48 insertions, 6 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py
index fa46a918b..3684ba492 100644
--- a/numpy/core/_internal.py
+++ b/numpy/core/_internal.py
@@ -267,3 +267,21 @@ class _ctypes(object):
shape = property(get_shape, None, doc="c-types shape")
strides = property(get_strides, None, doc="c-types strides")
_as_parameter_ = property(get_as_parameter, None, doc="_as parameter_")
+
+
+# Given a datatype and an order object
+# return a new names tuple
+# with the order indicated
+def _newnames(datatype, order):
+ oldnames = datatype.names
+ nameslist = list(oldnames)
+ if isinstance(order, str):
+ order = [order]
+ if isinstance(order, (list, tuple)):
+ for name in order:
+ try:
+ nameslist.remove(name)
+ except ValueError:
+ raise ValueError, "unknown field name: %s" % (name,)
+ return tuple(list(order) + nameslist)
+ raise ValueError, "unsupported order value: %s" % (order,)
diff --git a/numpy/core/src/arraymethods.c b/numpy/core/src/arraymethods.c
index 488ed3bf7..b48c0c02b 100644
--- a/numpy/core/src/arraymethods.c
+++ b/numpy/core/src/arraymethods.c
@@ -852,14 +852,38 @@ array_sort(PyArrayObject *self, PyObject *args, PyObject *kwds)
int axis=-1;
int val;
PyArray_SORTKIND which=PyArray_QUICKSORT;
- static char *kwlist[] = {"axis", "kind", NULL};
+ PyObject *order=NULL;
+ PyArray_Descr *saved=NULL;
+ PyArray_Descr *newd;
+ static char *kwlist[] = {"axis", "kind", "order", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&O", kwlist, &axis,
+ PyArray_SortkindConverter, &which,
+ &order))
+ return NULL;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&", kwlist, &axis,
- PyArray_SortkindConverter, &which))
- return NULL;
+ if (order != NULL) {
+ PyObject *new_name;
+ saved = self->descr;
+ if (saved->names == NULL) {
+ PyErr_SetString(PyExc_ValueError, "Cannot specify " \
+ "order with no fields.");
+ return NULL;
+ }
+ new_name = PyObject_CallMethod(_numpy_internal, "_newnames",
+ "OO", saved, order);
+ if (new_name == NULL) return NULL;
+ newd = PyArray_DescrNew(saved);
+ newd->names = new_name;
+ self->descr = newd;
+ }
- val = PyArray_Sort(self, axis, which);
- if (val < 0) return NULL;
+ val = PyArray_Sort(self, axis, which);
+ if (order != NULL) {
+ Py_XDECREF(self->descr);
+ self->descr = saved;
+ }
+ if (val < 0) return NULL;
Py_INCREF(Py_None);
return Py_None;
}