
	/* sort a group of targets by priority first, then obs. window start time */

#include <stdio.h>
#include <math.h>
#include "list.h"

void sort_groups( /* struct group *list[], int num_grp */ );
static int compar_grp( /* struct group *first, *second */ );

	/* do the sorting, using the UNIX qsort routine. */

void
sort_groups(list, num_grp)
struct s_group *list[];
int num_grp;
{
	int compar_grp();

	qsort((char *) &(list[0]), num_grp, sizeof(struct s_group *), compar_grp);
	
}

	/* return -1 if the first group's priority is less than the second's;
	          +1                                  greater than

	   if the two priorities are equal, then return -1 if the END of
	   the first's first observing window is earlier than the second's.
	   return +1 if the first's observing window is later than the second's;

	   if they start at the same time, then use the START time as the
	   criterion - return -1 if the start of the first's first observing window
	   is earlier than the second's, +1 if the first's observing window
	   starts later, and 0 otherwise. */

static int
compar_grp(first, second)
struct s_group **first, **second;
{
	if ((*first)->priority < (*second)->priority)
		return(-1);
	else if ((*first)->priority > (*second)->priority)
		return(1);
	else {
		if ((*first)->jd_end[0] < (*second)->jd_end[0])
			return(-1);
		else if ((*first)->jd_end[0] > (*second)->jd_end[0])
			return(1);
		else {
			if ((*first)->jd_start[0] < (*second)->jd_start[0])
				return(-1);
			else if ((*first)->jd_start[0] > (*second)->jd_start[0])
				return(1);
			else
				return(0);
		}
	}
}

