Index: /trunk/psphot/src/psphotBasicDeblend.c
===================================================================
--- /trunk/psphot/src/psphotBasicDeblend.c	(revision 13005)
+++ /trunk/psphot/src/psphotBasicDeblend.c	(revision 13006)
@@ -19,5 +19,5 @@
 
     // we need sources spatially-sorted to find overlaps
-    sources = psArraySort (sources, psphotSortByY);
+    sources = psArraySort (sources, pmSourceSortByY);
 
     // source analysis is done in peak order (brightest first)
Index: /trunk/psphot/src/psphotBlendFit.c
===================================================================
--- /trunk/psphot/src/psphotBlendFit.c	(revision 13005)
+++ /trunk/psphot/src/psphotBlendFit.c	(revision 13006)
@@ -13,5 +13,5 @@
 
     // source analysis is done in S/N order (brightest first)
-    sources = psArraySort (sources, psphotSortBySN);
+    sources = psArraySort (sources, pmSourceSortBySN);
     
     // S/N limit to perform full non-linear fits
Index: /trunk/psphot/src/psphotDeblendSatstars.c
===================================================================
--- /trunk/psphot/src/psphotDeblendSatstars.c	(revision 13005)
+++ /trunk/psphot/src/psphotDeblendSatstars.c	(revision 13006)
@@ -13,5 +13,5 @@
 
     // we need sources spatially-sorted to find overlaps
-    sources = psArraySort (sources, psphotSortByY);
+    sources = psArraySort (sources, pmSourceSortByY);
 
     // source analysis is done in peak order (brightest first)
Index: /trunk/psphot/src/psphotEnsemblePSF.c
===================================================================
--- /trunk/psphot/src/psphotEnsemblePSF.c	(revision 13005)
+++ /trunk/psphot/src/psphotEnsemblePSF.c	(revision 13006)
@@ -24,5 +24,5 @@
 
     // source analysis is done in spatial order
-    refSources = psArraySort (refSources, psphotSortByY);
+    refSources = psArraySort (refSources, pmSourceSortByY);
 
     // storage arrays for fitSources and sequence index
Index: /trunk/psphot/src/psphotSortBySN.c
===================================================================
--- /trunk/psphot/src/psphotSortBySN.c	(revision 13005)
+++ /trunk/psphot/src/psphotSortBySN.c	(revision 13006)
@@ -1,6 +1,39 @@
 # include "psphotInternal.h"
 
+
 // sort by SN (descending)
-int psphotSortBySN (const void **a, const void **b)
+int pmPeakSortBySN (const void **a, const void **b)
+{
+    pmPeak *A = *(pmPeak **)a;
+    pmPeak *B = *(pmPeak **)b;
+
+    psF32 fA = A->SN;
+    psF32 fB = B->SN;
+    if (isnan (fA)) fA = 0;
+    if (isnan (fB)) fB = 0;
+
+    psF32 diff = fA - fB;
+    if (diff > FLT_EPSILON) return (-1);
+    if (diff < FLT_EPSILON) return (+1);
+    return (0);
+}
+
+// sort by Y (ascending)
+int pmPeakSortByY (const void **a, const void **b)
+{
+    pmPeak *A = *(pmPeak **)a;
+    pmPeak *B = *(pmPeak **)b;
+
+    psF32 fA = A->y;
+    psF32 fB = B->y;
+
+    psF32 diff = fA - fB;
+    if (diff > FLT_EPSILON) return (+1);
+    if (diff < FLT_EPSILON) return (-1);
+    return (0);
+}
+
+// sort by SN (descending)
+int pmSourceSortBySN (const void **a, const void **b)
 {
     pmSource *A = *(pmSource **)a;
@@ -19,5 +52,5 @@
 
 // sort by Y (ascending)
-int psphotSortByY (const void **a, const void **b)
+int pmSourceSortByY (const void **a, const void **b)
 {
     pmSource *A = *(pmSource **)a;
Index: /trunk/psphot/src/psphotSourcePlots.c
===================================================================
--- /trunk/psphot/src/psphotSourcePlots.c	(revision 13005)
+++ /trunk/psphot/src/psphotSourcePlots.c	(revision 13006)
@@ -14,5 +14,5 @@
 
     // examine PSF sources in S/N order (brightest first)
-    sources = psArraySort (sources, psphotSortBySN);
+    sources = psArraySort (sources, pmSourceSortBySN);
 
     // counters to track the size of the image and area used in a row
Index: /trunk/psphot/src/psphotTestPSF.c
===================================================================
--- /trunk/psphot/src/psphotTestPSF.c	(revision 13005)
+++ /trunk/psphot/src/psphotTestPSF.c	(revision 13006)
@@ -17,5 +17,5 @@
 
     // examine PSF sources in S/N order (brightest first)
-    sources = psArraySort (sources, psphotSortBySN);
+    sources = psArraySort (sources, pmSourceSortBySN);
 
     // array to store candidate PSF stars
Index: /trunk/psphot/src/psphotWeightBias.c
===================================================================
--- /trunk/psphot/src/psphotWeightBias.c	(revision 13005)
+++ /trunk/psphot/src/psphotWeightBias.c	(revision 13006)
@@ -15,5 +15,5 @@
 
     // source analysis is done in S/N order (brightest first)
-    sources = psArraySort (sources, psphotSortBySN);
+    sources = psArraySort (sources, pmSourceSortBySN);
 
     // set fitting method to use non-poisson errors (local sky error)
