from .binding import sort as _sort

import numpy
from numpy.lib.recfunctions import append_fields

try:
    unicode = unicode
except NameError:
    # 'unicode' is undefined, must be Python 3
    str = str
    unicode = str
    bytes = bytes
    basestring = (str,bytes)
else:
    # 'unicode' exists, must be Python 2
    str = str
    unicode = unicode
    bytes = str
    basestring = basestring

def sort(source, orderby, out=None, comm=None):
    key = orderby
    if isinstance(key, basestring):
        return _sort(source, key, out, comm=comm)

    data1 = numpy.empty(
            len(source), dtype=[
                ('data', (source.dtype, source.shape[1:])),
                ('index', (key.dtype, key.shape[1:]))])

    data1['data'][...] = source
    data1['index'][...] = key

    _sort(data1, orderby='index', comm=comm)

    if out is None:
        out = source

    out[...] = data1['data'][...]


from numpy.testing import Tester
test = Tester().test
bench = Tester().bench

@@ -790,92 +790,375 @@
     
-_sort(data1, orderby='index', comm=comm)%0A%0A    if out is None:%0A        out = source%0A%0A
+if out is None:%0A        out = source%0A        _sort(data1, orderby='index', comm=comm)%0A        out%5B...%5D = data1%5B'data'%5D%5B...%5D%0A    else:%0A        data2 = numpy.empty(%0A            len(out), dtype=%5B%0A                ('data', (out.dtype, out.shape%5B1:%5D)),%0A                ('index', (key.dtype, key.shape%5B1:%5D))%5D)%0A        _sort(data1, orderby='index', out=data2, comm=comm)%0A    
     
@@ -1164,33 +1164,33 @@
  out%5B...%5D = data
-1
+2
 %5B'data'%5D%5B...%5D%0A%0A%0A

Fix out place mismatched out array size.