本文介绍了我有一个国家名单.我想按字母顺序对它进行排序,除了两个我想放在第一位的国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个国家清单.我想按字母顺序对它进行排序,除了两个我想放在第一位的国家.有没有一种简单的方法来修改
I have a list of countries. I'd like to sort it alphabetically, except for two countries which I'd like to put first. Is there a simple way to modify
from country in Countries
orderby country.Name
select country
要做到这一点?
推荐答案
尝试一下:
var specialCountries = new[] { "America", "England" };
from country in Countries
orderby specialCountries.Contains(country.Name) descending, country.Name
select country
如果这不是LINQ-to-Objects,则可能需要以稍微不同的方式编写它才能起作用,例如这可能有效
If this isn't LINQ-to-Objects, you might need to write it slightly differently for it to work, e.g. this might work
from country in Countries
orderby (country.Name == "America" || country.Name == "England") descending, country.Name
select country
这篇关于我有一个国家名单.我想按字母顺序对它进行排序,除了两个我想放在第一位的国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!