#include "m.h" static double *qv; static int compare( const void *ij1, const void *ij2) { int j1, j2; j1 = *(double *)ij1; j2 = *(double *)ij2; if( qv[j1] < qv[j2]) return -1; if( qv[j1] > qv[j2]) return 1; return 0; } Symbolptr sort( Symbolptr a) { Symbolptr v; int j, count, index; index = IO.u.val; count = ROWS(a) * COLS(a); v = m_create( 1, count); if( SCALAR(v)) { VAL(v) = index; return v; } for( j = 0; j < count; ++j) V(0,j) = j; qv = MAT(a)[0]; /* this does not handle possible row pointer swapping... */ /* * v contains indices of a. qsort() will sort v, not a. * The resulting indices can then be used to select elements * of a in increasing order. compare() access a through the * static qv pointer. */ qsort( MAT(v)[0], count, sizeof(double), compare); if( index != 0) for( j = 0; j < count; ++j) /* fix IO */ V(0,j) += index; return v; }