Is there any built-in C# support for doing an index sort?
More Details:
I have several sets of data stored in individual generic Lists of double.
These ar
Once you have set up the index array, you can sort it using a custom Comparison<T>
that compares the values in the corresponding items in the data array:
Array.Sort<int>(index, (a,b) => anylist[a].CompareTo(anylist[b]));
The following code achievs an indexed sort. Note the ToArray() call to clone the data array. If omited, the data array becomes sorted, too.
static void Main(String[] args)
{
Int32[] data = new Int32[] { -6, 6, 5, 4, 1, 2, 3, 0, -1, -2, -3, -4, -5 };
Int32[] indices = Enumerable.Range(0, data.Length).ToArray();
Array.Sort(data.ToArray(), indices);
foreach (Int32 index in indices)
{
Console.Write(String.Format("{0} ", data[index]));
}
Console.ReadLine();
}
The output is as exspected.
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6