1. ## Strange Sorting Method

Hello,

I have an array, declared as such:
Code:
`double a[8];`
a[0] through a[7] contain numbers that I would like to sort. Were this simply the issue, I'm sure I could find a function on Google. But what's important here is that I don't care about the final sorted array: I need a list of indecies from the original array in the order of the final array. For instance, say:
Code:
```a[0]=5;
a[1]=7;
a[2]=2;```
Then I don't want my result to be "2,5,7", I want it to be 2,0,1: the original indicies.

Does this make sense? How would I write this? I'm sure there must be a simple way that I'm missing.

Thanks a lot for your help,

2. one trick I have used is to use a second array of integers that contain the index values in the array you want to sort. When its time to swap elements, swap the integers in this second array, which keeps the original array in its original order.

In the code below you should use const int to represent array sizes, not hard-code them like I did.
Code:
```double a[8];
int i,j;
int index[8] = {0,1,2,3,4,5,6,7};

// simple bubble-sort algorithm
for( i = 0; i < 6; i++)
{
for( j = i+1; j < 7; j++)
{
if( a[index[i]] > a[index[j]])
{
int t = index[i];
index[i] = index[j];
index[j] = t;
}
}
}

// now list the array in sorted order
for(i = 0; i < 7; i++)
printf("%f  ", a[index[i]]);```

3. Originally Posted by crepincdotcom
Then I don't want my result to be "2,5,7", I want it to be 2,0,1: the original indicies.

Does this make sense? How would I write this? I'm sure there must be a simple way that I'm missing.
Something similar, FWIW, pretty much the same idea as AD.