using System;
using System.ComponentModel;
using System.Linq;

namespace BS.Shared.Extensions
{
    public static class BindingListExtensions
    {
        public static void Sort<T>(this BindingList<T> l, Func<T, T, int> sorter)
        {
            var temp = l.ToList();
            temp.Sort((x, y) => sorter(x, y));

            for (int i = 0; i < temp.Count; i++)
            {
                if (!l[i].Equals(temp[i]))
                {
                    l[i] = temp[i];
                }
            }
        }

        public static bool AreEqual<T>(this BindingList<T> list1, BindingList<T> list2)
        {
            if (list1.Count != list2.Count)
            {
                return false;
            }

            return !list1.Where((t, i) => !t.Equals(list2[i])).Any();
        }
    }
}