Index: /trunk/psLib/src/types/psArray.c
===================================================================
--- /trunk/psLib/src/types/psArray.c	(revision 15075)
+++ /trunk/psLib/src/types/psArray.c	(revision 15076)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.62 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-09-20 23:56:57 $
+ *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-28 21:03:24 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -31,4 +31,5 @@
 #include "psArray.h"
 #include "psLogMsg.h"
+#include "psAbort.h"
 #include "psAssert.h"
 
@@ -243,4 +244,59 @@
 }
 
+// Heap sort of the index array
+psVector *psArraySortIndex (psVector *out, psArray *in, psCompareFunc func) {
+
+    if (in == NULL) {
+        return NULL;
+    }
+
+    // XXX isn't this line a waste of space?
+    out = psVectorRecycle(out, in->n, PS_TYPE_S32); // Vector for output
+    out = psVectorCreate(out, 0, in->n, 1, PS_TYPE_S32);
+    long N = out->n;                    // Number of elements
+    if (N < 2) {
+        return out;
+    }
+
+    long I = N >> 1;
+    long J = N - 1;
+
+    int result;
+
+    psS32 temp;
+    psS32 *index = out->data.S32;
+    while (1) {
+        if (I > 0) {
+	    // I--;
+            temp = index[--I];
+        } else {
+            temp = index[J];
+            index[J] = index[0];
+	    // J--;
+            if (--J == 0) {
+                index[0] = temp;
+                return out;
+            }
+        }
+        long i = I;
+        long j = (I << 1) + 1;
+        while (j <= J) {
+	    result = func (in->data[index[j]], in->data[index[j+1]]);
+            if ((j < J) && (result < 0)) {
+                j++;
+	    }
+	    result = func (in->data[temp], in->data[index[j]]);
+            if (result < 0) {
+                index[i]=index[j];
+                j += (i=j) + 1;
+            } else {
+                j = J + 1;
+            }
+        }
+        index[i] = temp;
+    }
+    psAbort ("impossible to reach here");
+}
+
 psArray* psArraySort(psArray* array,
                      psComparePtrFunc func)
Index: /trunk/psLib/src/types/psArray.h
===================================================================
--- /trunk/psLib/src/types/psArray.h	(revision 15075)
+++ /trunk/psLib/src/types/psArray.h	(revision 15076)
@@ -10,6 +10,6 @@
  *  @author Joshua Hoblitt, University of Hawaii
  *
- *  @version $Revision: 1.50 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-09-20 23:56:57 $
+ *  @version $Revision: 1.51 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-28 21:03:24 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -21,4 +21,5 @@
 #include "psType.h"
 #include "psCompare.h"
+#include "psVector.h"
 
 /// @addtogroup DataContainer Data Containers
@@ -220,4 +221,6 @@
 );
 
+// return the index which sorts the array
+psVector *psArraySortIndex (psVector *outIndex, psArray *in, psCompareFunc func);
 
 /** Set an element in the array.  If the current element is non-NULL, the old
