37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AllBase.Extension
|
|
{
|
|
public static class SortExtension
|
|
{
|
|
public static void Sort<T>(this ObservableCollection<T> collection, Func<T, object> keySelector)
|
|
{
|
|
var sortedList = collection.OrderBy(keySelector).ToList();
|
|
for (int i = 0; i < collection.Count; i++)
|
|
{
|
|
if (!Equals(collection[i], sortedList[i]))
|
|
{
|
|
collection.Move(collection.IndexOf(sortedList[i]), i);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void SortDesc<T>(this ObservableCollection<T> collection, Func<T, object> keySelector)
|
|
{
|
|
var sortedList = collection.OrderByDescending(keySelector).ToList();
|
|
for (int i = 0; i < collection.Count; i++)
|
|
{
|
|
if (!Equals(collection[i], sortedList[i]))
|
|
{
|
|
collection.Move(collection.IndexOf(sortedList[i]), i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|