How can I sort only part of a huge list using .NET?

In .NET, generalization lists have a sort function that accepts ICompareror Comparison. I would like to sort only part of the list. I hope I can specify the starting index, the number of items to sort, and the lambda function. It looks like you can only use lambda functions for this if you sort the entire list. Is this correct or am I missing something?

Additional requirements:

  • In-place sorting (to save memory / time)
  • The end list is the same length as the original list
+3
source share
4 answers
List<int> mylist = new List<int>() {8,4,6,2,1,5,3,1,7};
List<int> myRange = mylist.GetRange(2,4);

mylist.RemoveRange(2, 4);
mylist.InsertRange(2,  myRange.OrderBy(i => i));

mylist.Dump();

EDIT: Dump foreach .
.

EDIT2: .,

    List<int> mylist = new List<int>() ;
    for(int i=9999999; i > 0; i--)
    {
        mylist.Add(i);
    }

    Console.WriteLine("start " + DateTime.Now.Ticks);
    var extract = mylist.Skip(10).Take(1000000).OrderBy(i => i);

    int k = 10; // start from (because we skipped from 10 onwards above)
    foreach(int item in extract)
    {
        mylist[k++] = item;
    }


    Console.WriteLine("done" + DateTime.Now.Ticks);
    foreach(int item in mylist)
        Console.WriteLine(item);
+1

RemoveRange InsertRange . , , , Sort, .

Dim myList As New List<int>() { 8, 4, 6, 2, 1, 5, 3, 1, 7 }
Dim myRange = myList.GetRange(2,4)

myRange.Sort(yourComparer)
Dim i = 2;
For Each item in myRange
    mylist(i) = item
    i += 1
Next
+1

linq, . FindAll.

genericList = genericList.FindAll(x => x.Field).OrderBy(y => y.Field).ToList();
0

, , ? , Linq - - .

IEnumerable<T> enumerable = GetTheListToWorkWith();
IEnumerable<T> sortedStuff = enumerable.Skip(startIndex).Take(endIndex).OrderBy(t => t.ValueToCompare);

: IEnumerable, .

Alternatively (if all data should remain in the list, you can delete the subset that you want to sort, then sort, then bind it to the original list again

0
source

Source: https://habr.com/ru/post/1735874/