C# 在C语言中,如何按对象的多个字段对对象集合进行排序?
我有一个项目对象列表,我想使用项目对象中的几个不同字段对集合进行排序C# 在C语言中,如何按对象的多个字段对对象集合进行排序?,c#,linq,sorting,collections,C#,Linq,Sorting,Collections,我有一个项目对象列表,我想使用项目对象中的几个不同字段对集合进行排序 IEnumerable<Project> list = GetProjectList(); public class Project { public DateTime? Date; public string ImportanceLevel; } 我首先想 按日期字段排序-与将来的日期相比,更早的日期应位于列表顶部,并且这些日期应显示在没有日期的任何项目之前。 对于没有设置日期的项
IEnumerable<Project> list = GetProjectList();
public class Project
{
public DateTime? Date;
public string ImportanceLevel;
}
我首先想
按日期字段排序-与将来的日期相比,更早的日期应位于列表顶部,并且这些日期应显示在没有日期的任何项目之前。
对于没有设置日期的项目,我想按重要级别进行排序,它可以是3个字符串中的一个,低、中或高,其中高会先到中再到低
如果这些是整数值,我很感激这会更好,但不幸的是,它们现在被存储为字符串。这将起作用
var orderedList = list
.OrderBy(p => p.Date)
.ThenByDescending(p =>
{
if (p.ImportanceLevel == "Low")
return 1;
if (p.ImportanceLevel == "Medium")
return 2;
if (p.ImportanceLevel == "Hight")
return 3;
return 0;
});
这会奏效的
var orderedList = list
.OrderBy(p => p.Date)
.ThenByDescending(p =>
{
if (p.ImportanceLevel == "Low")
return 1;
if (p.ImportanceLevel == "Medium")
return 2;
if (p.ImportanceLevel == "Hight")
return 3;
return 0;
});
一个选择是
var sorted = list.OrderBy(l => l.Date.HasValue ? l.Date : DateTime.MaxValue)
.ThenBy(l => l.ImportanceLevel == "High" ? 1 :
(l.ImportanceLevel == "Medium" ? 2 : 3));
在这里,它会做你想做的事情,也会按照重要性对相同日期的项目进行排序
//Order by the projects with Date
var projectsWithDate = list.Where(d => d.Date != null)
.OrderBy(s => s.Date).ToList();
// Projects without Dates
var withoutdate = list.Where(d => d.Date == null)
.OrderBy(g =>
{
if (g.ImportanceLevel == "High")
return 1;
if (g.ImportanceLevel == "Medium")
return 2;
if (g.ImportanceLevel == "Low")
return 3;
return 0;
})
.ToList();
//Add the second list to first.
projectsWithDate.AddRange(withoutdate);
或者
其中,它不会按重要性对有日期的项目进行排序。一个选项是
var sorted = list.OrderBy(l => l.Date.HasValue ? l.Date : DateTime.MaxValue)
.ThenBy(l => l.ImportanceLevel == "High" ? 1 :
(l.ImportanceLevel == "Medium" ? 2 : 3));
//Order by the projects with Date
var projectsWithDate = list.Where(d => d.Date != null)
.OrderBy(s => s.Date).ToList();
// Projects without Dates
var withoutdate = list.Where(d => d.Date == null)
.OrderBy(g =>
{
if (g.ImportanceLevel == "High")
return 1;
if (g.ImportanceLevel == "Medium")
return 2;
if (g.ImportanceLevel == "Low")
return 3;
return 0;
})
.ToList();
//Add the second list to first.
projectsWithDate.AddRange(withoutdate);
在这里,它会做你想做的事情,也会按照重要性对相同日期的项目进行排序
//Order by the projects with Date
var projectsWithDate = list.Where(d => d.Date != null)
.OrderBy(s => s.Date).ToList();
// Projects without Dates
var withoutdate = list.Where(d => d.Date == null)
.OrderBy(g =>
{
if (g.ImportanceLevel == "High")
return 1;
if (g.ImportanceLevel == "Medium")
return 2;
if (g.ImportanceLevel == "Low")
return 3;
return 0;
})
.ToList();
//Add the second list to first.
projectsWithDate.AddRange(withoutdate);
或者
其中,它不会按重要性对有日期的项目进行排序
//Order by the projects with Date
var projectsWithDate = list.Where(d => d.Date != null)
.OrderBy(s => s.Date).ToList();
// Projects without Dates
var withoutdate = list.Where(d => d.Date == null)
.OrderBy(g =>
{
if (g.ImportanceLevel == "High")
return 1;
if (g.ImportanceLevel == "Medium")
return 2;
if (g.ImportanceLevel == "Low")
return 3;
return 0;
})
.ToList();
//Add the second list to first.
projectsWithDate.AddRange(withoutdate);
//现在您可以使用projectsWithDate集合
//现在您可以使用projectsWithDate集合。确实应该是重复的-但是因为这里有这么多重复的答案已经不确定应该做什么…确实应该是重复的-但是因为这里有这么多重复的答案已经不确定应该做什么。。。