本文介绍了对列表进行排序,以便最终获得特定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类Offer
,其中包含一个已归档的类别.
I have a class Offer
which contains a filed Category.
我希望特定类别的所有商品都显示在顶部,然后是其他所有商品.
I want all Offers of a specific category to appear on top, followed by all else.
我尝试过,但无济于事,您会推荐什么?
I tried this, but to no avail, what would you recommend?
Offers = Offers.OrderBy(x => x.Category == "Corporate").ToList();
推荐答案
按布尔值排序时,false
(0)在true
(1)之前.要首先获取与谓词匹配的元素,应使用OrderByDescending
:
When you order by a boolean value false
(0) comes before true
(1). To get the elements that match the predicate first you should reverse the sort order by using OrderByDescending
:
Offers = Offers.OrderByDescending(x => x.Category == "Corporate").ToList();
这篇关于对列表进行排序,以便最终获得特定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!