C# a、 订货人( a=>t.InvokeMember( 财产秩序 ,System.Reflection.BindingFlags.GetProperty 无效的 A. 无效的 ) ).ToList(); } 其他的 { lista=lista.OrderByDescending( a=>t.InvokeMember( 财产秩序 ,System.Reflection.BindingFlags.GetProperty 无效的 A. 无效的 ) ).ToList(); } } }

C# a、 订货人( a=>t.InvokeMember( 财产秩序 ,System.Reflection.BindingFlags.GetProperty 无效的 A. 无效的 ) ).ToList(); } 其他的 { lista=lista.OrderByDescending( a=>t.InvokeMember( 财产秩序 ,System.Reflection.BindingFlags.GetProperty 无效的 A. 无效的 ) ).ToList(); } } },c#,generics,sorting,ilist,C#,Generics,Sorting,Ilist,这个问题启发我写了一篇博文: 我认为,理想情况下,.NET Framework将包含一个接受IList的静态排序方法,但下一个最好的方法是创建自己的扩展方法。创建两个方法并不难,它们允许您像排序列表一样对IList进行排序。作为奖励,您可以使用相同的技术重载LINQ OrderBy扩展方法,以便无论您使用的是List.Sort、IList.Sort还是IEnumerable.OrderBy,都可以使用完全相同的语法 public static class SortExtensions {

这个问题启发我写了一篇博文:

我认为,理想情况下,.NET Framework将包含一个接受IList的静态排序方法,但下一个最好的方法是创建自己的扩展方法。创建两个方法并不难,它们允许您像排序列表一样对IList进行排序。作为奖励,您可以使用相同的技术重载LINQ OrderBy扩展方法,以便无论您使用的是List.Sort、IList.Sort还是IEnumerable.OrderBy,都可以使用完全相同的语法

public static class SortExtensions
{
    //  Sorts an IList<T> in place.
    public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
    {
        ArrayList.Adapter((IList)list).Sort(new ComparisonComparer<T>(comparison));
    }

    // Sorts in IList<T> in place, when T is IComparable<T>
    public static void Sort<T>(this IList<T> list) where T: IComparable<T>
    {
        Comparison<T> comparison = (l, r) => l.CompareTo(r);
        Sort(list, comparison);

    }

    // Convenience method on IEnumerable<T> to allow passing of a
    // Comparison<T> delegate to the OrderBy method.
    public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, Comparison<T> comparison)
    {
        return list.OrderBy(t => t, new ComparisonComparer<T>(comparison));
    }
}

// Wraps a generic Comparison<T> delegate in an IComparer to make it easy
// to use a lambda expression for methods that take an IComparer or IComparer<T>
public class ComparisonComparer<T> : IComparer<T>, IComparer
{
    private readonly Comparison<T> _comparison;

    public ComparisonComparer(Comparison<T> comparison)
    {
        _comparison = comparison;
    }

    public int Compare(T x, T y)
    {
        return _comparison(x, y);
    }

    public int Compare(object o1, object o2)
    {
        return _comparison((T)o1, (T)o2);
    }
}
公共静态类SortExtensions
{
//将IList排序到位。
公共静态无效排序(此IList列表,比较)
{
Adapter((IList)list.Sort(新的ComparisonComparer(比较));
}
//当T是IComparable时,在IList中就地排序
公共静态空排序(此IList列表),其中T:IComparable
{
比较=(l,r)=>l.CompareTo(r);
排序(列表、比较);
}
//IEnumerable上允许传递
//比较委托给OrderBy方法。
公共静态IEnumerable OrderBy(此IEnumerable列表,比较)
{
return list.OrderBy(t=>t,newcomparisoncomparer(comparison));
}
}
//将常规比较委托包装在IComparer中以使其更容易
//对采用IComparer或IComparer的方法使用lambda表达式
公共类比较比较器:IComparer,IComparer
{
私有只读比较\u比较;
公共比较(比较)
{
_比较=比较;
}
公共整数比较(TX,TY)
{
返回-比较(x,y);
}
公共整数比较(对象o1、对象o2)
{
返回-比较((T)o1,(T)o2);
}
}
使用这些扩展,对IList进行排序,就像对列表进行排序一样:

IList<string> iList = new []
{
    "Carlton", "Alison", "Bob", "Eric", "David"
};

// Use the custom extensions:

// Sort in-place, by string length
iList.Sort((s1, s2) => s1.Length.CompareTo(s2.Length));

// Or use OrderBy()
IEnumerable<string> ordered = iList.OrderBy((s1, s2) => s1.Length.CompareTo(s2.Length));
IList-IList=new[]
{
“卡尔顿”、“艾莉森”、“鲍勃”、“埃里克”、“大卫”
};
//使用自定义扩展名:
//按字符串长度就地排序
iList.Sort((s1,s2)=>s1.Length.CompareTo(s2.Length));
//或者使用OrderBy()
IEnumerable ordered=iList.OrderBy((s1,s2)=>s1.Length.CompareTo(s2.Length));
帖子中有更多信息:

试试这个**按**使用订单:
公营雇员
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
}
私有静态IList GetItems()
{
List lst=新列表();
lst.Add(新员工{Id=“1”,Name=“Emp1”});
lst.Add(新员工{Id=“2”,Name=“Emp2”});
lst.Add(新员工{Id=“7”,Name=“Emp7”});
lst.Add(新员工{Id=“4”,Name=“Emp4”});
lst.Add(新员工{Id=“5”,Name=“Emp5”});
lst.Add(新员工{Id=“6”,Name=“Emp6”});
lst.Add(新员工{Id=“3”,Name=“Emp3”});
返回lst;
}
**var lst=GetItems().AsEnumerable();
var orderedLst=lst.OrderBy(t=>t.Id).ToList();
orderedLst.ForEach(emp=>Console.WriteLine(“Id-{0}Name-{1}”,emp.Id,emp.Name))**

DavidMills接受的答案很好,但我认为可以改进。首先,当框架已经包含一个静态方法
Comparer.Create(comparation)
时,不需要定义
ComparisonComparer
类。此方法可用于动态创建
i比较

此外,它将
IList
转换为
IList
,这可能会造成危险。在我所看到的大多数情况下,实现
IList
List
在后台用于实现
IList
,但这并不能保证,并且可能会导致脆弱的代码

最后,重载的
List.Sort()
方法有4个签名,只实现了其中的2个

  • List.Sort()
  • List.Sort(比较)
  • List.Sort(IComparer)
  • List.Sort(Int32、Int32、IComparer)
  • 下面的类实现了
    IList
    接口的所有4个
    List.Sort()
    签名:

    using System;
    using System.Collections.Generic;
    
    public static class IListExtensions
    {
        public static void Sort<T>(this IList<T> list)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort();
            }
            else
            {
                List<T> copy = new List<T>(list);
                copy.Sort();
                Copy(copy, 0, list, 0, list.Count);
            }
        }
    
        public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort(comparison);
            }
            else
            {
                List<T> copy = new List<T>(list);
                copy.Sort(comparison);
                Copy(copy, 0, list, 0, list.Count);
            }
        }
    
        public static void Sort<T>(this IList<T> list, IComparer<T> comparer)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort(comparer);
            }
            else
            {
                List<T> copy = new List<T>(list);
                copy.Sort(comparer);
                Copy(copy, 0, list, 0, list.Count);
            }
        }
    
        public static void Sort<T>(this IList<T> list, int index, int count,
            IComparer<T> comparer)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort(index, count, comparer);
            }
            else
            {
                List<T> range = new List<T>(count);
                for (int i = 0; i < count; i++)
                {
                    range.Add(list[index + i]);
                }
                range.Sort(comparer);
                Copy(range, 0, list, index, count);
            }
        }
    
        private static void Copy<T>(IList<T> sourceList, int sourceIndex,
            IList<T> destinationList, int destinationIndex, int count)
        {
            for (int i = 0; i < count; i++)
            {
                destinationList[destinationIndex + i] = sourceList[sourceIndex + i];
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    公共静态类
    {
    公共静态无效排序(此IList列表)
    {
    如果(列表是列表)
    {
    ((List)List.Sort();
    }
    其他的
    {
    列表副本=新列表(列表);
    copy.Sort();
    复制(复制,0,列表,0,列表.计数);
    }
    }
    公共静态无效排序(此IList列表,比较)
    {
    如果(列表是列表)
    {
    ((列表)列表)、排序(比较);
    }
    其他的
    {
    列表副本=新列表(列表);
    复制、排序(比较);
    复制(复制,0,列表,0,列表.计数);
    }
    }
    公共静态无效排序(此IList列表、IComparer比较器)
    {
    如果(列表是列表)
    {
    ((列表)列表)。排序(比较器);
    }
    其他的
    {
    列表副本=新列表(列表);
    复制.排序(比较器);
    复制(复制,0,列表,0,列表.计数);
    }
    }
    公共静态无效排序(此IList列表,int索引
    
    public class Widget {
        public string Name = string.Empty;
        public int Size = 0;
    
        public Widget(string name, int size) {
        this.Name = name;
        this.Size = size;
    }
    }
    
    public class WidgetNameSorter : IComparer<Widget> {
        public int Compare(Widget x, Widget y) {
            return x.Name.CompareTo(y.Name);
    }
    }
    
    public class WidgetSizeSorter : IComparer<Widget> {
        public int Compare(Widget x, Widget y) {
        return x.Size.CompareTo(y.Size);
    }
    }
    
    List<Widget> widgets = new List<Widget>();
    widgets.Add(new Widget("Zeta", 6));
    widgets.Add(new Widget("Beta", 3));
    widgets.Add(new Widget("Alpha", 9));
    
    widgets.Sort(new WidgetNameSorter());
    widgets.Sort(new WidgetSizeSorter());
    
    using System.Linq;
    
    var yourList = SomeDAO.GetRandomThings();
    yourList.ToList().Sort( (thing, randomThing) => thing.CompareThisProperty.CompareTo( randomThing.CompareThisProperty ) );
    
     public class FormatCcdeSorter:IComparer<ReportFormat>
        {
           public int Compare(ReportFormat x, ReportFormat y)
            {
               return x.FormatCode.CompareTo(y.FormatCode);
            }
        }
    
    ReportFormat[] myReports = new ReportFormat[reports.Count]; //reports is the merged IList
    
    Array.Sort(myReports, new FormatCodeSorter());//sorting using custom comparer
    
            IList<string> ilist = new List<string>();
            ilist.Add("B");
            ilist.Add("A");
            ilist.Add("C");
    
            Console.WriteLine("IList");
            foreach (string val in ilist)
                Console.WriteLine(val);
            Console.WriteLine();
    
            List<string> list = (List<string>)ilist;
            list.Sort();
            Console.WriteLine("List");
            foreach (string val in list)
                Console.WriteLine(val);
            Console.WriteLine();
    
            list = null;
    
            Console.WriteLine("IList again");
            foreach (string val in ilist)
                Console.WriteLine(val);
            Console.WriteLine();
    
        List<MeuTeste> temp = new List<MeuTeste>();
    
        temp.Add(new MeuTeste(2, "ramster", DateTime.Now));
        temp.Add(new MeuTeste(1, "ball", DateTime.Now));
        temp.Add(new MeuTeste(8, "gimm", DateTime.Now));
        temp.Add(new MeuTeste(3, "dies", DateTime.Now));
        temp.Add(new MeuTeste(9, "random", DateTime.Now));
        temp.Add(new MeuTeste(5, "call", DateTime.Now));
        temp.Add(new MeuTeste(6, "simple", DateTime.Now));
        temp.Add(new MeuTeste(7, "silver", DateTime.Now));
        temp.Add(new MeuTeste(4, "inn", DateTime.Now));
    
        SortList(ref temp, SortDirection.Ascending, "MyProperty");
    
        private void SortList<T>(
        ref List<T> lista
        , SortDirection sort
        , string propertyToOrder)
        {
            if (!string.IsNullOrEmpty(propertyToOrder)
            && lista != null
            && lista.Count > 0)
            {
                Type t = lista[0].GetType();
    
                if (sort == SortDirection.Ascending)
                {
                    lista = lista.OrderBy(
                        a => t.InvokeMember(
                            propertyToOrder
                            , System.Reflection.BindingFlags.GetProperty
                            , null
                            , a
                            , null
                        )
                    ).ToList();
                }
                else
                {
                    lista = lista.OrderByDescending(
                        a => t.InvokeMember(
                            propertyToOrder
                            , System.Reflection.BindingFlags.GetProperty
                            , null
                            , a
                            , null
                        )
                    ).ToList();
                }
            }
        }
    
    public static class SortExtensions
    {
        //  Sorts an IList<T> in place.
        public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
        {
            ArrayList.Adapter((IList)list).Sort(new ComparisonComparer<T>(comparison));
        }
    
        // Sorts in IList<T> in place, when T is IComparable<T>
        public static void Sort<T>(this IList<T> list) where T: IComparable<T>
        {
            Comparison<T> comparison = (l, r) => l.CompareTo(r);
            Sort(list, comparison);
    
        }
    
        // Convenience method on IEnumerable<T> to allow passing of a
        // Comparison<T> delegate to the OrderBy method.
        public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, Comparison<T> comparison)
        {
            return list.OrderBy(t => t, new ComparisonComparer<T>(comparison));
        }
    }
    
    // Wraps a generic Comparison<T> delegate in an IComparer to make it easy
    // to use a lambda expression for methods that take an IComparer or IComparer<T>
    public class ComparisonComparer<T> : IComparer<T>, IComparer
    {
        private readonly Comparison<T> _comparison;
    
        public ComparisonComparer(Comparison<T> comparison)
        {
            _comparison = comparison;
        }
    
        public int Compare(T x, T y)
        {
            return _comparison(x, y);
        }
    
        public int Compare(object o1, object o2)
        {
            return _comparison((T)o1, (T)o2);
        }
    }
    
    IList<string> iList = new []
    {
        "Carlton", "Alison", "Bob", "Eric", "David"
    };
    
    // Use the custom extensions:
    
    // Sort in-place, by string length
    iList.Sort((s1, s2) => s1.Length.CompareTo(s2.Length));
    
    // Or use OrderBy()
    IEnumerable<string> ordered = iList.OrderBy((s1, s2) => s1.Length.CompareTo(s2.Length));
    
    try this  **USE ORDER BY** :
    
       public class Employee
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }
    
     private static IList<Employee> GetItems()
            {
                List<Employee> lst = new List<Employee>();
    
                lst.Add(new Employee { Id = "1", Name = "Emp1" });
                lst.Add(new Employee { Id = "2", Name = "Emp2" });
                lst.Add(new Employee { Id = "7", Name = "Emp7" });
                lst.Add(new Employee { Id = "4", Name = "Emp4" });
                lst.Add(new Employee { Id = "5", Name = "Emp5" });
                lst.Add(new Employee { Id = "6", Name = "Emp6" });
                lst.Add(new Employee { Id = "3", Name = "Emp3" });
    
                return lst;
            }
    
    **var lst = GetItems().AsEnumerable();
    
                var orderedLst = lst.OrderBy(t => t.Id).ToList();
    
                orderedLst.ForEach(emp => Console.WriteLine("Id - {0} Name -{1}", emp.Id, emp.Name));**
    
    using System;
    using System.Collections.Generic;
    
    public static class IListExtensions
    {
        public static void Sort<T>(this IList<T> list)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort();
            }
            else
            {
                List<T> copy = new List<T>(list);
                copy.Sort();
                Copy(copy, 0, list, 0, list.Count);
            }
        }
    
        public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort(comparison);
            }
            else
            {
                List<T> copy = new List<T>(list);
                copy.Sort(comparison);
                Copy(copy, 0, list, 0, list.Count);
            }
        }
    
        public static void Sort<T>(this IList<T> list, IComparer<T> comparer)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort(comparer);
            }
            else
            {
                List<T> copy = new List<T>(list);
                copy.Sort(comparer);
                Copy(copy, 0, list, 0, list.Count);
            }
        }
    
        public static void Sort<T>(this IList<T> list, int index, int count,
            IComparer<T> comparer)
        {
            if (list is List<T>)
            {
                ((List<T>)list).Sort(index, count, comparer);
            }
            else
            {
                List<T> range = new List<T>(count);
                for (int i = 0; i < count; i++)
                {
                    range.Add(list[index + i]);
                }
                range.Sort(comparer);
                Copy(range, 0, list, index, count);
            }
        }
    
        private static void Copy<T>(IList<T> sourceList, int sourceIndex,
            IList<T> destinationList, int destinationIndex, int count)
        {
            for (int i = 0; i < count; i++)
            {
                destinationList[destinationIndex + i] = sourceList[sourceIndex + i];
            }
        }
    }
    
    class Foo
    {
        public int Bar;
    
        public Foo(int bar) { this.Bar = bar; }
    }
    
    void TestSort()
    {
        IList<int> ints = new List<int>() { 1, 4, 5, 3, 2 };
        IList<Foo> foos = new List<Foo>()
        {
            new Foo(1),
            new Foo(4),
            new Foo(5),
            new Foo(3),
            new Foo(2),
        };
    
        ints.Sort();
        foos.Sort((x, y) => Comparer<int>.Default.Compare(x.Bar, y.Bar));
    }
    
        var ordered = theIList.Cast<T>().OrderBy(e => e);
    
      ObservableCollection<Plugin.ContactService.Shared.Contact> ContactItems= new ObservableCollection<Contact>();
    
        foreach (var item in ordered)
        {
           ContactItems.Add(item);
        }