c# - 使用 IComparable 进行反向排序

标签 c# sorting icomparable

我有这样的代码 -

List<User> users;
protected class User : IComparable<User>
{
    public string name;
    public string email;
    public decimal total;
    public string address;
    public string company;
    public string origin;
    public int CompareTo(User b)
    {
        return this.total.CompareTo(b.total);

    }
}

对于按用户拥有的点数排序的表格。它按升序排序,但需要将其更改为降序。它使用 users.Sort(),但我似乎无法弄清楚如何让它按相反的顺序排序。

最佳答案

如果要颠倒顺序,只需颠倒比较即可:

public int CompareTo(User b)
{
    return b.total.CompareTo(this.total);
}

关于c# - 使用 IComparable 进行反向排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25063701/

相关文章:

c# - C# wpf中用datagrid信息填充MYSQL数据库

c# - 使用 itextsharp 在 pdf 文件中画线的问题

c# - 在我的游戏中使用什么代替 Application.DoEvents?

C++使用移位操作对数字进行排序

c# - Windows 窗体程序中的多个 UI

mysql - 按触发器排序排名

java - 无法添加逻辑来对 Java/android 中现有比较器中的日期进行排序

c# - IComparable 是在字典中强制使用唯一键的最佳方式吗?

c# - 带参数的自定义 Icomparer

c# - 为什么 IComparable<T> 不继承自 IComparable?