CSharp examples for System.Collections.Generic:IList
Uses an insertion sort algorithm to perform a stable sort IList (keep the initial order of the keys with equal values).
using System.Text; using System.Collections.Generic; using System;//from w w w.j a v a 2s .com public class Main{ /// <summary> /// Uses an insertion sort algorithm to perform a stable sort (keep the initial order of the keys with equal values). /// </summary> /// <remarks>Memory overhead is null, average complexity is O(n.ln(n)), worst-case is O(n?).</remarks> /// <typeparam name="T"></typeparam> /// <param name="src"></param> /// <param name="comparison"></param> public static void StableSort<T>(this IList<T> list, Comparison<T> comparison) { // For every key for (int i = 1; i < list.Count; i++) { var value = list[i]; int j = i - 1; // Move the key backward while the previous items are lesser than it, shifting those items to the right while (j >= 0 && comparison(list[j], value) > 0) { list[j + 1] = list[j]; j--; } // Insert at the left of the scrolled sequence, immediately on the right of the first lesser or equal value it found list[j + 1] = value; } } /// <summary> /// Uses an insertion sort algorithm to perform a stable sort (keep the initial order of the keys with equal values). /// </summary> /// <remarks>Memory overhead is null, average complexity is O(n.ln(n)), worst-case is O(n?).</remarks> /// <typeparam name="T"></typeparam> /// <param name="src"></param> /// <param name="comparison"></param> public static void StableSort<T>(this IList<T> list, IComparer<T> comparer) { list.StableSort(comparer.Compare); } }