Fastest way to find index of elements in an array`

Ok, so i have a float array, containing say 100 elements (*please note, i'm not using vectors).

Now, to the best of my limited knowledge, i haven't found a way to sort my array into size order, and then return the indices of the elements. So, i've implemented the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
floatfixedarray array2 = array1; // so make a copy of the original array
int n = array1.GetLength(); // find the number of elements in the array

sort(array1.Start(), array1.Start()+n);  // so this sorts the distance out in order - but i want the indices of these (this uses std::sort)

intfixedarray compare(n);

	for (int j = 0; j < n; j++)
	{
		for (int i = 0; i < n; i++)
		{
			if (array1[j] == array2[i]) 
                        {
                              compare[j] = i;
                              break; // this stops the loop from looking through the rest of the values for no good reason
                        }
		}
	}



where array1 is a float array, array2 is a copy of the original array1, and compare is an int array. (i have created my own array classes).

So - compare will give me the indices of the elements in array1 after they have been sorted.
This works, but i was wondering if there was a better / faster way of doing this?
Last edited on
I think I would make array2 an array of a struct containing the original index and the original value from array1, and then I would overload operator< (this would be the requirement to be able to use sort(), I think). This should then give me the whole thing.
Topic archived. No new replies allowed.