1

I am trying to sort different item arrays based on one key array. The following simple code represents my more complex code:

int[] first = new int[] { 1, 9, 2, 3, 8, 4, 5 };
string[] second = new string[] { "one", "nine", "two", "three", "eight", "four", "five" };

int[] temp = first;
Array.Sort(temp, second);

foreach (int v in temp)
{
    Debug.WriteLine(v.ToString());
}

foreach (string v in second)
{
    Debug.WriteLine(v);
}

int[] third = new int[] { 11, 99, 22, 33, 88, 44, 55 };

foreach (int v in first)
{
    Debug.WriteLine(v.ToString());
}

Array.Sort(first, third);

foreach (int v in first)
{
    Debug.WriteLine(v.ToString());
}

foreach (int v in third) 
{
    Debug.WriteLine(v);
}

The arrays, called 'second' and 'third', should be sorted based on the ordering of array 'first'. I found that I can do this with:

Array.Sort(first, second)

This perfectly works, until I add another Array.Sort to sort 'third'. Since I want to keep 'first' as key array for other sorting actions, I use a temporary array called 'temp' to save the initial sequence for 'first' so that I can reuse each time. When I reuse first to also sort 'third' using Array.Sort(first, third), the sorting does not work (see output). It seems that 'first' gets sorted together with 'temp' during the first Array.Sort, even though it is not in the command.

Output:

1
2
3
4
5
8
9

one
two
three
four
five
eight
nine

1   //--> 'first' before it is used in Array.Sort, so it seems already sorted
2
3
4
5
8
9

1
2
3
4
5
8
9

11  //--> 'third' does not get sorted because 'first' seemed already sorted
99
22
33
88
44
55

How do I make sure that my key array does not get sorted so that I can use it multiple times?

4
  • public static void Sort(Array keys, Array items); It will sort based on keys, which present in first array, which was already sorted. Commented Jul 14, 2017 at 10:08
  • @Nilay: have you read the question? Commented Jul 14, 2017 at 10:09
  • yes @TimSchmelter, you can see in debug array first is already sorted and used as key for that method so it does not sort it's item array. Try with out sorting firstArray. Commented Jul 14, 2017 at 10:13
  • @Nilay: Array "first" was initially not sorted and my question was why "first" got sorted even though I didn't use "first" in the first Array.Sort. Instead I used "temp" and still "first" was sorted, even without a specific command from my side. InBetween's answer explains what I did wrong. Commented Jul 14, 2017 at 11:36

2 Answers 2

3

An array is a reference type. A variable is a placeholder for a value. The value stored in a reference type variable is the "address" (so to speak) in memory of the object it is referencing. When you assign the value of one reference type variable to another you are simply copying the value stored in the variable.

So what does this have to do with anything? Everything!

int[] temp = first;

This line here is not doing what you think it should. It is simply copying the value stored in first to the variable named temp. And what was that value? Yup, the address of the array, so now, both temp and first point to the exact same array; so, whatever changes you make to the array vía temp will also change the array referenced by first because both are the same array.

You need to create a fresh copy of the array each time. The easiest way to do that is shown in Pablo notPicasso's answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this clarification!
3

Create new array:

Array.Sort(first.ToArray(), second);
Array.Sort(first.ToArray(), third);

3 Comments

I would imagine first.Clone() would be better since I would expect it to be optimised for arrays which ToArray() probably won't be (though its possible it is).
@Chris true, but IClonable is simply such a mess (you are never sure if it does what it should) that I simply run away from it whenever I can. ToArray is clear what it does, and unless there is a performance reason to do it otherwise, I like this solution better.
@Chris - .ToArray() is optimized for arrays. It's effectively an Array.Copy call.

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.