Standard practice would be to sort such data through an index or pointer array. I'll describe how to use an index:
Make an int (unsigned long if needed), with at least as many elements as there are rows of data in the file to be sorted.
Now initialize the index[] (array), so each number, matches it's current position number, in the index[]:
for(i=0;i<SIZE;i++) //SIZE is the number of elements in the index[]
index[i] = i; //simple, no?
Now, you're ready to start sorting, but instead of sorting (comparing and moving) the actual data, you'll make both of these, by going through your index. If you haven't seen it before, it's a bit confusing.
<< NO data is moved, whatsoever >>
Upon completion (and it's fast), you can print out (on screen, or to file, etc.), all the rows, in sorted order.
You guessed it - by using the now sorted, index. Here's what the final print out to screen might look like:
for(i=0;i<SIZE;i++)
printf("%5d ", data[index[i]]);
This is an example of Insertion sort, using an index array:
/* Demonstrates Insertion Sort by using an index[] array.
Status: OK
Adak December 20, 2010
Requires a file named "soup.txt" with this format:
Tomato 2.28 245678
Broth 1.60 313926
Pea 2.35 455092
Stew 3.85 210420
Noodle 2.41 110288
Vegetable 2.33 699240
*/
#include <stdio.h>
#include <string.h>
#define SIZE 15
void sort(float num[], int lo, int hi);
int main() {
int i,ok;
float num[SIZE];
char str[25];
unsigned long num_id;
FILE *fp;
printf("\n\n\n");
fp=fopen("soup1.txt", "r");
if(fp==NULL) {
printf("Error opening file - Correct file name is soup.txt \n");
printf("File format is up to SIZE rows with 3 columns:\n\
Name (1 word) Price (float) and 6 digit unsigned long product ID number\n");
printf(" Column separators are spaces\n");
return 1;
}
i=ok=0;
printf("\n Name Price Product ID\n");
printf (" =======================================\n");
while(1) {
ok=fscanf(fp, "%s %f %lu%*c", str, &num[i], &num_id);
if(ok < 1)
break;
printf(" %-10s %6.2f %lu\n", str, num[i], num_id);
++i;
}
fclose(fp);
putchar('\n');
sort(num, 0, i); //sort num[], through the index
printf("\n\n\t\t\t press enter when ready\n");
(void) getchar();
return 0;
}
/* Insertion sort, through an index[] array */
void sort(float A[], int lo, int hi) {
int i, j; //the indeces for the array locations
float val; //sorting floats here
int idx[15]; //the index array
for(i=0;i<SIZE;i++) //initialize the index array
idx[i] = i;
printf("\nThe Prices of Soups in their Original Order:\n");
for(i=lo;i<hi;i++) printf("%5.2f ",A[idx[i]]); getchar();
for(i=lo+1;i<hi;i++) {
val = A[idx[i]]; //get a value of A[], through the index array
j = i-1;
while(A[idx[j]] > val) { //the fast insertion sort "shuffle"
idx[j + 1] = idx[j]; //only the index int's are moved
--j; //look Ma! no temp variable ;)
if(j<0) break;
}
idx[j+1] = i; //drop it into place
}
printf("\nThe Prices of Soups After Insertion Sort:\n");
for(i=lo;i<hi;i++) printf("%5.2f ",A[idx[i]]);
getchar();
}
/* Just an example of an Insertion sort function, without an index array.
void insertionSort(int A[], int lo, int hi) {
int i, j, val;
for(i=lo+1;i<hi;i++) {
val = A[i];
j = i-1;
while(A[j] > val) {
A[j + 1] = A[j];
--j;
if(j<0) break;
}
A[j+1] = val;
}
}
*/