c# - 对列表进行排序,使特定值排在最前面

标签 c# linq list generics

我有一个 Offer 类,其中包含一个已归档的类别。

我希望特定类别的所有报价显示在顶部,然后是其他所有报价。

我试过了,但没有用,你有什么推荐的?

Offers = Offers.OrderBy(x => x.Category == "Corporate").ToList();

最佳答案

当您按 bool 值排序时,false (0) 出现在 true (1) 之前。要首先获取与谓词匹配的元素,您应该使用 OrderByDescending 反转排序顺序:

Offers = Offers.OrderByDescending(x => x.Category == "Corporate").ToList();

关于c# - 对列表进行排序,使特定值排在最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8436857/

相关文章:

c# - 类、结构或接口(interface)成员声明中的无效标记 '=' c#

c# - 对鼠标移动有即时 react 的线条图?

c# - SqlBulkCopy 上传Excel

c# - 林克/C# : How to split List into variable length chunks based on list item information?

.net - 使用通过 LINQ 查询的 SQL Server 中的超时异常

python - 优化: Search the best way to compare two list of dict (Python)

c# - 了解结构的 "this"参数(特别是迭代器/异步)

c# - 某处有 Ix.NET (System.Interactive) 的示例吗?

Python 2.7 - 为什么 python 在列表中使用 .append() 时对字符串进行编码?

c# - 没有任何内容添加到对象列表中