CoffeDeveloper
CoffeDeveloper

Reputation: 8315

C# sorted LinkedList

It seems I'm not able to find the C# (C# 3.0 I'm using Unity3D) collection I need:

  1. I need to be able to add and remove items quickly while iterating like in a LinkedList.
  2. I need to sort it frequently several times like Sort in List (interested in any of mergesort, quicksort and radixsort)
  3. I need to sort according to different sort functions because I have to sort according to different fields like Name, Surname etc. (so SortedList or SortedDictionary no good).

List, seems "almost correct" apart the fact it is in reality an array and I cannot remove items randomly while iterating forward.

LinkedList would be perfect but miss sorting ability (for a linkedlist would be natural mergesort, wich is in addition stable, may need it to be a stable sort, not required for now).

The best bet would be finding that actually a .NET sorting method already exists for LinkedList and I'm not aware of it.

If I would have time of implementing such class myself I would go for a LinkedList with a method for sorting using a mergesort and a custom lambda as comparer, however seems that stuff is missing from .NET?

Upvotes: 2

Views: 2377

Answers (1)

maraaaaaaaa
maraaaaaaaa

Reputation: 8163

Linq!

  1. I need to be able to add and remove items quickly while iterating like in a LinkedList.

use the linq collection.RemoveAll(item => {conditions} );

  1. I need to sort it frequently several times like Sort in List (interested in any of mergesort, quicksort and radixsort)

use the linq .OrderBy(item => item.Field).ThenBy(item => item.OtherField).ThenByDescending(item => item.OmgAnotherField);

  1. I need to sort according to different sort functions because I have to sort according to different fields like Name, Surname etc. (so SortedList or SortedDictionary no good).

see #2

Upvotes: 3

Related Questions