Beispiel #1
0
    /// <summary>
    /// Constructor, requires generic List of Ts
    /// where T must implement CompareTo() method,
    /// And Compare method to use when sorting,
    /// (Overrides default CompareTo() implemented by T) ...
    /// </summary>
    /// <param name="list">List of T elements to sort</param>
    /// <param name="compareDelegate">Method to use to compare elements</param>
    public QuickSort(IList <T> list, CompareDlg <T> compareDelegate)
        : this()
    {
        if (list.Count == 0)
        {
            throw new InvalidOperationException(
                      "Empty List passed to QuickSort.");
        }
        var first = default(T);

        if (typeof(T).IsClass)
        {
            foreach (var t in list)
            {
                if (!((first = t).Equals(default(T))))
                {
                    break;
                }
            }
            if (first.Equals(default(T)))
            {
                throw new InvalidOperationException(
                          "List passed to QuickSort contains all nulls.");
            }
        }
        if (compareDelegate == null && !(first is IComparable <T>))
        {
            throw new InvalidOperationException(string.Format(
                                                    "Type {0} does not implement IComparable<{0}>. " +
                                                    "Generic Type T must either implement IComparable " +
                                                    "or a comparison delegate must be provided.", typeof(T)));
        }
        itms = list;
        cmp += compareDelegate ?? CompareDefault;
    }
Beispiel #2
0
 public static void Sort(IList <T> itms, CompareDlg <T> compareDelegate)
 {
     (new QuickSort <T>(itms, compareDelegate)).Sort();
 }