10

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.

2 Answers 2

16

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]));
Sign up to request clarification or add additional context in comments.

3 Comments

No, there is no LINQ, but there is a lambda expression which is commonly used with LINQ.
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]));
@Geotarget: That's the array that the OP describes in the question.
0

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

3 Comments

No, that is not an index sort, eventhough part of the result from it is the same as from an index sort.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.