Search code examples
c#genericssql-order-byicomparericomparablet

Order list<T> on a numerical base


i am using this code to order a list descending on numerical base

ItemsList.OrderByDescending(x => x.Views, new IntComparer());

public class IntComparer : IComparer<long>
{
    IComparer<long> Members;

    public int Compare(long x, long y)
    {
        return Math.Sign(x - y);
    }
}

but it doesn't order at all :S any help plz


Solution

  • Enumerable.OrderByDescending is part of LINQ.

    So it is not modifying the list, but it is creating new one. Use

    ItemsList = ItemsList.OrderByDescending(x => x.Views, new IntComparer()).ToList();
    

    or something similiar.