Ibrahim F Haddad wrote:

> Given a file that contains data of the following format:
> 
> 7.606565 5.404950
> 4.969272 4.597600
> 4.969272 4.597600
> 2.400000 3.486746
> 3.000000 3.672252
> 3.600000 3.766663
> 
> 
> How to sort it in increasing order of the x column so
> after sorting it looks like that:
> 
> 2.400000 3.486746
> 3.000000 3.672252
> 3.600000 3.766663
> 4.969272 4.597600
> 4.969272 4.597600
> 7.606565 5.404950
> 
> please note that duplicate entries may exist.

In this specific case, you could just sort the file with `sort -n'.

More generally, you can sort arbitrary data using the `qsort'
function, which is part of the ANSI C standard library (qsort is
declared in stdlib.h).

Supposing you have read the data into an array of doubles, where row i 
is stored in elements 2i and 2i+1, you can sort the array using:

static int compare(const void *pa, const void *pb)
{
        double a = *(const double *)pa;
        double b = *(const double *)pb;

        if (a < b)
                return -1;
        if (a > b)
                return 1;
        return 0;
}

void sort_it(double *data, int rows)
{
        qsort(data, rows, 2*sizeof(double), compare);
}

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to