#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

# include "psphot.h"

// sort by SN (descending)
int psphotSortBySN (const void **a, const void **b)
{
    pmSource *A = *(pmSource **)a;
    pmSource *B = *(pmSource **)b;

    psF32 fA = (A->moments == NULL) ? 0 : A->moments->SN;
    psF32 fB = (B->moments == NULL) ? 0 : B->moments->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 psphotSortByY (const void **a, const void **b)
{
    pmSource *A = *(pmSource **)a;
    pmSource *B = *(pmSource **)b;

    psF32 fA = (A->moments == NULL) ? 0 : A->moments->y;
    psF32 fB = (B->moments == NULL) ? 0 : B->moments->y;

    psF32 diff = fA - fB;
    if (diff > FLT_EPSILON) return (+1);
    if (diff < FLT_EPSILON) return (-1);
    return (0);
}

