How to sort the part of a list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 15, 6, 19, 9, 7, 3, 8, 1, 4 };

        // Sort a portion of the list (e.g., elements at index 2 to 6)
        numbers.Sort(2, 5, Comparer<int>.Default); // 19, 9, 7, 3, 8 -> 3, 7, 8, 9, 19

        // Print the sorted list
        Console.WriteLine(string.Join(", ", numbers));
    }
}




/*
run:

15, 6, 3, 7, 8, 9, 19, 1, 4

*/

 



answered Aug 11, 2025 by avibootz
...