views:

611

answers:

3

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 are lists always equal in length, and hold corresponding data items, but these lists come and go dynamically, so I can't just store corresponding data items in a class or struct cleanly. (I'm also dealing with some legacy issues.)

I need to be able to sort these keyed from any one of the data sets.

My thought of the best way to do this is to add one level of indirection, and use an index based sort. Such sorts have been in use for years.

Quick definition of index based sort :
make "index", an array of consecutive integers the same length as the lists, then the sort algorithm sorts the list of integers so that anylist[index[N]] gives the Nth item of anylist in sorted order. The lists themselves are never re-ordered.

Is there any built-in C# support for doing an index sort? I have been unable to find it... everything I have found reorders the collection itself. My guess is support exists but I haven't looked in the right place yet.

I am using C#.NET 3.5, under Windows.

A: 

I think you'd have to write your own sort-comparison delegate and perform the dereference within it. With 3.5 lambda functions this should be pretty simple and clean...

Jeff Kotula
+4  A: 

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]));
Guffa
Wow! That is amazing. Does exactly what I needed in one line. I assume this must be using Linq. I haven't studied it and so don't know how it works... guess I need to study some. Thanks!
Mark T
No, there is no LINQ, but there is a lambda expression which is commonly used with LINQ.
Guffa
And if your array isn't comparable, its just 1 more line: Comparer<T> comparer = Comparer<T>.Default; Array.Sort<int>(index, (a, b) => comparer.Compare(array[a],array[b]));
John Gardner
A: 

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
Daniel Brückner
No, that is not an index sort, eventhough part of the result from it is the same as from an index sort.
Guffa
The items in indices have become reordered so that data[indices[i]] for i in 0 to data.length yields the elements of data in sorted order. This is what an index sort does.
Daniel Brückner
I admit that the way it is done is not really smart because the indices are sorted by sorting a copy of the data. The result is the same but expensive compared to your solution if the data items are large.
Daniel Brückner