Index: trunk/Ohana/src/libdvo/Makefile
===================================================================
--- trunk/Ohana/src/libdvo/Makefile	(revision 15750)
+++ trunk/Ohana/src/libdvo/Makefile	(revision 16040)
@@ -36,4 +36,5 @@
 $(SRC)/version.$(ARCH).o	 \
 $(SRC)/coordops.$(ARCH).o	 \
+$(SRC)/dvosorts.$(ARCH).o	 \
 $(SRC)/dvo_photcode_ops.$(ARCH).o \
 $(SRC)/dvo_photcode_convert_elixir.$(ARCH).o \
Index: trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- trunk/Ohana/src/libdvo/include/dvo.h	(revision 15750)
+++ trunk/Ohana/src/libdvo/include/dvo.h	(revision 16040)
@@ -412,3 +412,10 @@
 int        SkyTableSetFilenames    PROTO((SkyTable *sky, char *path, char *ext));
 
+/* dvo-specific sorting functions */
+void sortave (Average *ave, int N);
+void sort_image_subset (Image *image, int *subset, int N);
+void sort_coords_index (double *X, double *Y, int *S, int N);
+void sort_stars_ra (Stars *stars, int N);
+void sort_regions (SkyRegion *region, int N);
+
 # endif
Index: trunk/Ohana/src/libdvo/src/dvosorts.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvosorts.c	(revision 16040)
+++ trunk/Ohana/src/libdvo/src/dvosorts.c	(revision 16040)
@@ -0,0 +1,71 @@
+# include <dvo.h>
+
+/* several dvo-specific sorting functions used in a number of locations */
+
+/* values are ave[i].R, ave[i].D, ave[i].M */
+void sortave (Average *ave, int N) {
+
+# define SWAPFUNC(A,B){ Average tmp; tmp = ave[A]; ave[A] = ave[B]; ave[B] = tmp; }
+# define COMPARE(A,B)(ave[A].R < ave[B].R)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+/* sort subset by image[subset[i]].tzero */
+void sort_image_subset (Image *image, int *subset, int N) {
+
+# define SWAPFUNC(A,B){ int tmp; tmp = subset[A]; subset[A] = subset[B]; subset[B] = tmp; }
+# define COMPARE(A,B)(image[subset[A]].tzero < image[subset[B]].tzero)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+/* sort a coordinate pair (X,Y) and the associated index (S) */
+void sort_coords_index (double *X, double *Y, int *S, int N) {
+  
+# define SWAPFUNC(A,B){ double dtmp; int itmp; \
+  dtmp = X[A]; X[A] = X[B]; X[B] = dtmp; \
+  dtmp = Y[A]; Y[A] = Y[B]; Y[B] = dtmp; \
+  itmp = S[A]; S[A] = S[B]; S[B] = itmp; \
+}
+# define COMPARE(A,B)(X[A] < X[B])
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void sort_stars_ra (Stars *stars, int N) {
+
+# define SWAPFUNC(A,B){ Stars tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].R < stars[B].R)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
+void sort_regions (SkyRegion *region, int N) {
+
+# define SWAPFUNC(A,B){ SkyRegion tmp; tmp = region[A]; region[A] = region[B]; region[B] = tmp; }
+# define COMPARE(A,B)(region[A].Dmin < region[B].Dmin)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+
Index: trunk/Ohana/src/libdvo/src/skyregion_gsc.c
===================================================================
--- trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 15750)
+++ trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 16040)
@@ -206,47 +206,20 @@
 void SkyTableSort (SkyTable *table) {
 
-  int i, j, l, ir, N;
   SkyRegion *regions;
-  SkyRegion tempregion;
-  char *tempfile = NULL;
   char **filename = NULL;
-  
-  N = table[0].Nregions;
+
   regions = table[0].regions;
   filename = table[0].filename;
 
-  if (N < 2) return;
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tempregion = regions[l];
-      if (filename) tempfile   = filename[l];
-    } else {
-      tempregion   = regions[ir];
-      if (filename) tempfile     = filename[ir];
-      regions[ir]   = regions[0];
-      if (filename) filename[ir] = filename[0];
-      if (--ir == 0) {
-	regions[0]   = tempregion;
-	if (filename) filename[0] = tempfile;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && regions[j].Rmin < regions[j+1].Rmin) ++j;
-      if (tempregion.Rmin < regions[j].Rmin) {
-	regions[i]   = regions[j];
-	if (filename) filename[i] = filename[j];
-	j += (i=j) + 1;
-      } 
-      else j = ir + 1;
-    }
-    regions[i]   = tempregion;
-    if (filename) filename[i] = tempfile;
-  }
+# define SWAPFUNC(A,B){ int tmp; \
+  char     *tempfile = filename[A]; filename[A] = filename[B]; filename[B] = tempfile; \
+  SkyRegion tempregion = regions[A]; regions[A] = regions[B]; regions[B] = tempregion; \
+}
+# define COMPARE(A,B)(regions[A].Rmin < regions[B].Rmin)
+
+  OHANA_SORT (table[0].Nregions, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
 }
 
