c# - 使用 List.Sort(Comparison<T> comparison) 在 C# 中对列表进行排序

标签 c# .net list sorting

我创建了一个类如下:

public class StringMatch
{
   public int line_num;
   public int num_of_words;
}

我已经创建了一个列表

List<StringMatch> sm;

里面的元素很少。

如何使用 Comparison<T> 对列表进行排序比较过载? 必须根据 num_of_words 进行排序字段。

最佳答案

您可以编写 lambda 表达式来比较两个对象,如下所示:

sm.Sort((x,y)=>x.num_of_words.CompareTo(y.num_of_words));

你可以通过添加-

进行逆序排序
sm.Sort((x,y)=>-x.num_of_words.CompareTo(y.num_of_words));

关于c# - 使用 List.Sort(Comparison<T> comparison) 在 C# 中对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20902248/

相关文章:

c# - 在WEB API 2中模拟WCF路由

c# - BuildSiteMap() 方法如何工作?

c# - 从 Angular 4 SPA 请求 Web API 时范围声明为空

c# - 托管在 Windows 服务中的 wcf 服务,我有哪些缓存选项?

.net - 个人身份信息 (PII)、跟踪和消息记录

c# - MSBuild 未将文件复制到发布文件夹?

python - 中序二叉树遍历(使用Python)

jquery - 使用 jQuery 对列表项进行动画处理

java - 当我使用列表时出现奇怪的空指针异常错误

c# - 插入 View 返回受影响的 2 行