score:7

Accepted answer

Try something like this:

var partiallySorted = list.Where(x => x < 11)
                 .Concat(list.Where(x => x >= 11 && x <=15).OrderBy(/*blah*/)))
                 .Concat(list.Where(x => x > 15));

score:2

Simply get the required range based on some criteria and apply the sort on the resultant range using Linq.

List<int> numbers = new List<int>() { 15, 4, 1, 3, 2, 11, 7, 6, 12, 13 };
var range = numbers.Skip(3).Take(4).OrderBy(n => n).Select(s => s);
// output: 2, 3, 7, 11

score:4

List<int> list = new List<int>() {1,2,3,15,11,13,10,6,7};
list.Sort(3, 4,Comparer<int>.Default);

score:0

No, the Linq extension methods will never modify the underlying list. You can use the method List<T>.Sort(int index, int counter, IComparer<T> comparer) to do an in-place sort:

var list = new List<int> {1, 2, 3, 15, 11, 13, 10, 6, 7};
list.Sort(3, 4, null);

score:1

Use this for default inline List Sort:

Syntax: List.Sort(start index, number of elements, Default Comparer)

List<int> numbers = new List<int> { 1, 2, 3, 15, 11, 13, 10, 6, 7 };
numbers.Sort(3, 6, Comparer<int>.Default);

If you want to sort by [properties/attributes] of the element or precisely something else use the below method,

I had sorted the string by number of characters, and also from 2nd element to end of List.

Syntax: List.Sort(start index, number of elements, Custom Comparer)

List<string> str = new List<string> { "123", "123456789", "12", "1234567" };
str.Sort(1, str.Count - 1, Comparer<string>.Create((x, y) => x.Length.CompareTo(y.Length)));

Related Articles