using System.Collections;
using System.Collections.Generic;
public abstract class Sorter<T>
public abstract void SortAsc(IList<T> list, Comparer<T> comparer);
public class BubbleSorter<T> : Sorter<T>
public override void SortAsc(IList<T> collection, Comparer<T> comparer)
comparer = comparer ?? Comparer<T>.Default;
for (int i = 0; i < collection.Count; i++)
for (int index = 0; index < collection.Count - i - 1; index++)
if (comparer.Compare(collection[index], collection[index + 1]) > 0)
Swap(collection, index, index + 1);
void Swap(IList<T> list, int firstIndex, int secondIndex)
if (list.Count < 2 || firstIndex == secondIndex)
var temp = list[firstIndex];
list[firstIndex] = list[secondIndex];
list[secondIndex] = temp;
public static class Lister
public static void SortAsc<T>(this IList<T> list, Sorter<T> sorter = null, Comparer<T> comparer = null)
sorter = sorter ?? new BubbleSorter<T>();
sorter.SortAsc(list, comparer);
public static void Main()
var array = new int[] { 3, 2, 0, 1 };
array.SortAsc(new BubbleSorter<int>());
foreach(var a in array) {