Sorting Sections of a List

Today I had to tackle the problem of sorting only a section of a list, not the entire thing. Fortunately, Sort() has an overload that already does this for you:

using System;
using System.Collections.Generic;

namespace PartialSorts
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            List<int> values = new List<int>() { 5, 2, 8, 6, 1 };
            values.Sort(1, 3, null);
            
            foreach(int value in values)
            {
                Console.Out.WriteLine(value);
            }
        }
    }
}

Running the code produces this:

5
2
6
8
1

Note that the 2nd, 3rd, and 4th values were sorted, but the 1st and 5th stayed where they are. Nice!

* Posted at 01.17.2008 02:06:52 PM CST | Link *

Blog History