gpt4 book ai didi

c# - 按字母顺序排列列表,但某些元素始终位于顶部

转载 作者:行者123 更新时间:2023-11-30 14:25:52 26 4
gpt4 key购买 nike

class Employee   
{
static void Main(string[] args)
{
List<favorite> f = new List<favorite>();

f.Add(new favorite {id = 1, title ="MEN" });
f.Add(new favorite { id = 2, title = "WOMEN" });
f.Add(new favorite { id = 3, title = "BOYS" });
f.Add(new favorite { id = 4, title = "GIRLS" });
f.Add(new favorite { id = 5, title = "Ajay" });
f.Add(new favorite { id = 6, title = "vijay" });
f.Add(new favorite { id = 7, title = "Jitu" });
f.Add(new favorite { id = 8, title = "Suresh" });
f.Add(new favorite { id = 9, title = "Ramesh" });
f.Add(new favorite { id = 10, title = "Akshay" })

foreach (var item in f.OrderBy(e=>e.title).ThenBy(e=>e.title))
{
Console.WriteLine(item.title);
}
Console.ReadLine();
}

class favorite
{
public int id { get; set; }
public string title { get; set; }
}
}

上面的列表有 4 个默认收藏夹(MENWOWENBOYSGIRLS),我希望始终位于顶部,所有其他收藏夹按字母顺序排列。

例子:我希望输出像

MEN
WOMEN
BOYS
GIRLS
AJAY
AKSHAY
JITU
LIKE
and so on ...

我想使用 Entity Framework Lambda 表达式执行此查询。
我尝试使用 OrderByThenBy 来做到这一点,但还没有找到合适的解决方案。

最佳答案

如果你不想改变 favorite 类,你可以在某处定义这样的函数,

private static int GetPrimaryOrder(string title)
{
switch (title)
{
case "MEN":
return 1;
case "WOMEN":
return 2;
case "BOYS":
return 3;
case "GIRLS":
return 4;
default:
return 5;
}
}

并将其用于初始订购,例如,

foreach (var item in f.OrderBy(x => GetPrimaryOrder(x.title)).ThenBy(e => e.title))
{
Console.WriteLine(item.title);
}

关于c# - 按字母顺序排列列表,但某些元素始终位于顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com