```/* nag_rank_sort (m01dsc) Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
*
* Mark 26.1, 2017.
*
*/

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nag_stddef.h>
#include <nagm01.h>

#ifdef __cplusplus
extern "C"
{
#endif
static Integer NAG_CALL compare(const Nag_Pointer a, const Nag_Pointer b);
#ifdef __cplusplus
}
#endif

int main(void)
{
Integer exit_status = 0;
NagError fail;
double *vec = 0;
ptrdiff_t step;
size_t i, n, *rank = 0, step_u;

INIT_FAIL(fail);

/* Skip heading in data file */
scanf("%*[^\n]");
printf("nag_rank_sort (m01dsc) Example Program Results\n\n");
scanf("%" NAG_UFMT "%" NAG_UFMT "", &n, &step_u);
step = (ptrdiff_t) step_u;
if (n >= 1) {
if (!(vec = NAG_ALLOC(n, double)) || !(rank = NAG_ALLOC(n, size_t))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
}
else {
printf("Invalid n or step.\n");
exit_status = 1;
return exit_status;
}
for (i = 0; i < n; ++i)
scanf("%lf", &vec[i]);
/* nag_rank_sort (m01dsc).
* Rank sort of set of values of arbitrary data type
*/
nag_rank_sort((Pointer) vec, n, step * (ptrdiff_t) (sizeof(double)),
compare, Nag_Ascending, rank, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rank_sort (m01dsc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("    Data      Rank\n");
for (i = 0; i < n; ++i)
printf("   %7.4f   %4" NAG_UFMT "\n", vec[i], rank[i]);
END:
NAG_FREE(vec);
NAG_FREE(rank);
return exit_status;
}

static Integer NAG_CALL compare(const Nag_Pointer a, const Nag_Pointer b)
{
double x = *((const double *) a);
double y = *((const double *) b);
return (x < y ? -1 : (x == y ? 0 : 1));
}
```