9

This code snippet is from C# 2010 for Dummies. What confuses me is that when using the Array.Sort() method, both my copy of the array (sortedNames) and the original array (planets) get sorted, even though it only calls the Sort method on sortedNames.

It doesn't matter which array the second foreach loop references, the output is the same.

static void Main(string[] args)
{
    Console.WriteLine("The 5 planets closest to the sun, in order: ");
    string[] planets = new string[] { "Mercury","Venus", "Earth", "Mars", "Jupiter"};
    foreach (string planet in planets)
    {
        Console.WriteLine("\t" + planet);
    }
    Console.WriteLine("\nNow listed alphabetically: ");


    string[] sortedNames = planets;
    Array.Sort(sortedNames);

    foreach (string planet in planets)
    {
        Console.WriteLine("\t" + planet);
    }
}
A.L
  • 10,259
  • 10
  • 67
  • 98
Bill Nazzaro
  • 127
  • 1
  • 6

2 Answers2

22

Both sortedNames and planets refer to the same array. Basically both variables point to the same location in memory, so when you call Array.Sort on either variable, the changes to the array are reflected by both variables.

Since arrays in C# are reference types, both sortedNames and planets "point" to the same location in memory.

Contrast this with value types, which hold data within their own memory allocation, instead of pointing to another location in memory.

If you wanted to keep planets intact, you could use create a brand new array, then use Array.Copy to fill the new array with the contents of planets:

/* Create a new array that's the same length as the one "planets" points to */
string[] sortedNames = new string[planets.Length];

/* Copy the elements of `planets` into `sortedNames` */
Array.Copy(planets, sortedNames, planets.Length);

/* Sort the new array instead of `planets` */
Array.Sort(sortedNames);

Or, using LINQ you could use OrderBy and ToArray to create a new, ordered array:

string[] sortedNames = planets.OrderBy(planet => planet).ToArray();

Some resources that might help with value types and reference types:

Community
  • 1
  • 1
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
0

Alternatively you could use Array.Clone to avoid having to first create a new array. Then sort.

string[] sortedNames = (string[]) Array.Clone(planets);
Array.Sort(sortedNames);

See also this other discussion Difference between the System.Array.CopyTo() and System.Array.Clone()

HaC
  • 902
  • 12
  • 24