CSharp examples for System.Collections.Generic:IEnumerable
Stable Sort IEnumerable
using System.Collections.Generic; using System.Collections; public class Main{ internal static IEnumerable<T> StableSort<T>(IEnumerable<T> items, IComparer<T> comparer) {/*from w w w. j av a 2 s.c o m*/ #if NET35 // LINQ's OrderBy is guaranteed to be a stable sort. return items.OrderBy(x => x, comparer); #else // otherwise, tag each item with the index and sort the wrappers. // if we're given a collection (and we always are), use its count // to prevent unnecessary array copies. var itemCollection = items as ICollection<T>; var taggedItems = itemCollection == null ? new List<IndexTaggedItem<T>>() : new List<IndexTaggedItem<T>>(itemCollection.Count); int index = 0; foreach (var item in items) { taggedItems.Add(new IndexTaggedItem<T>(item, index++)); } taggedItems.Sort(new IndexAwareComparer<T>(comparer)); var sorted = new List<T>(taggedItems.Count); foreach (var taggedItem in taggedItems) { sorted.Add(taggedItem.Item); } return sorted; #endif } internal static IEnumerable<T> StableSort<T>(IEnumerable<T> items) { return StableSort(items, Comparer<T>.Default); } }