Site Search
Homepage of Otaku No Zoku
Complete Archives of Otaku No Zoku
About Otaku No Zoku
Subscribe to Otaku No Zoku
Bookmark Otaku No Zoku

C# Array.Sort Range Of Elements Example :

The .NET framework provides a built-in method for sorting a portion of an array on the few occasions when you need it. Again, it is trivially easy to use, simply specifying the array to sort, the index of the element of where to start sorting from, and the number of elements to sort.

Bear in mind that array indices are zero-based, so the very first element in the array, in this case 42 (which also happens to be the answer to the meaning of liff*, the universe, and everything), has an index of zero.

If you attempt to sort more elements in the array than actually exist, e.g. setting the third parameter, which is length, to a value of 12 for instance, the Array.Sort method will throw an argument exception.

* Not a spelling mistake.

Sorting A Range of Elements Within An Array

This program sorts a portion of an array of unsorted integers. The array contains ten integers and initially all elements of the array are unsorted. Using the built-in .NET Array.Sort method the first five elements of the array are sorted in to their natural numerical order, and the last five elements remain untouched and hence unsorted.

Steps

  1. Allocate a small array of unsorted integers.
  2. Use the Array.Sort method to sort only a portion of the integers
  3. Print the entire array, displaying both the sorted part and the unsorted part of the array
  4. Print the sorted part of the array for comparison
  5. Print the unsorted part of the array for comparison

The Source Code

using System;

class RangeSort
{
    static void Main()
    {
        // allocate a small array of ten integers
        int[] numericalValues = { 42, 7, 19, 99, 57, 25, 45, 12, 37, 28 };

        // sort only a part of the array in to natural numerical order
        // and leave the rest of the array untouched
        Array.Sort(numericalValues, 0, 5);

        //output each number in the array
        Console.WriteLine(String.Format("Entire array: {0}", String.Join(", ", numericalValues)));

        //output just the sorted part of the array
        Console.WriteLine(String.Format("Sorted portion: {0}", String.Join(", ", new List(numericalValues).GetRange(0, 5))));

        //output the remaining unsorted part of the array
        Console.WriteLine(String.Format("Unsorted portion: {0}", String.Join(", ", new List(numericalValues).GetRange(5, 5))));
    }
}

Program Output

Entire array: 7, 19, 43, 57, 99, 25, 45, 12, 37, 28
Sorted portion: 7, 19, 43, 57, 99
Unsorted portion: 25, 45, 12, 37, 28

Results

The output displays not only the entire array, showing the first five sorted elements and also the following five unsorted elements, but the following two lines show the sorted portion of the array, and the unsorted portion of the array, respectively.

Summary

The Array.Sort method is a versatile function that can do much more than just perform a straight linear sort of elements in an Array. Exploring the method overloads (17 in .NET 4.0) is worth the time to find out just how versatile and powerful this function actually is.

Further Reading

Liked This Post?

Subscribe to the RSS feed or follow me on Twitter to stay up to date!