自义定c#集合的排序
class Program
{
public class Stu
{
public string name { get; set; }
public string grade { get; set; }
}
static void Main(string[] args)
{
List<Stu> stuList = new List<Stu>();
stuList.Add(new Stu
{
name = "张三",
grade = "高级"
});
stuList.Add(new Stu
{
name = "李四",
grade = "初级"
});
stuList.Add(new Stu
{
name = "王五",
grade = "中级"
});
stuList.Sort((o1, o2) =>
{
//此处可换成字典
int ParseGrade(string grade)
{
int value = 0;
switch (grade)
{
case "初级":
value = 1;
break;
case "中级":
value = 2;
break;
case "高级":
value = 3;
break;
default:
break;
}
return value;
}
//降序
//return ParseGrade(o2.grade) - ParseGrade(o1.grade);
//升序
return ParseGrade(o1.grade) - ParseGrade(o2.grade);
});
}
}