示例#1
0
        private void Sort(IEnumerable items, string sortBy)
        {
            List <ListBoxItemData> arrayList = (List <ListBoxItemData>)items;
            int length = arrayList.Count - 1;

            _PropertyInfo property = typeof(ListBoxItemData).GetProperty(sortBy);

            Func <object, object, int> ComparisonFunction;

            switch (sortBy)
            {
            case "Name":
            case "Type":
            case "Project":
            case "Path":
                ComparisonFunction = StringCompareTo;
                break;

            case "LineCount":
                ComparisonFunction = IntCompareTo;
                break;

            case "DateModified":
                ComparisonFunction = DateTimeCompareTo;
                break;

            case "Size":
                ComparisonFunction = SizeCompareTo;
                break;

            default:
                throw (new NotImplementedException("Unhandled sort parameter."));
            }

            while (true)
            {
                bool _swapMade = false;

                for (int i = 0; i < length; i++)
                {
                    ListBoxItemData i0 = arrayList[i];
                    ListBoxItemData i1 = arrayList[i + 1];

                    if (ComparisonFunction(property.GetValue(i0, null), property.GetValue(i1, null)) > 0)
                    {
                        arrayList[i + 1] = i0;
                        arrayList[i]     = i1;
                        _swapMade        = true;
                    }
                }

                if (!_swapMade)
                {
                    break;
                }
            }

            Dispatcher.BeginInvoke(() =>
            {
                files.ItemsSource = null;
                files.ItemsSource = arrayList;
            });
        }