这篇文章介绍了C#中对集合排序的三种方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
对集合排序,可能最先想到的是使用OrderBy方法。
class Program
{
static void Main(string[] args)
{
IEnumerable<Student> result = GetStudents().OrderBy(r => r.Score);
foreach (var item in result)
{
Console.WriteLine(item.Name + "--" + item.Score);
}
Console.ReadKey();
}
private static List<Student> GetStudents()
{
return new List<Student>()
{
new Student(){Id = 1, Name = "张三",Age = 15, Score = 80},
new Student(){Id = 2, Name = "李四",Age = 16, Score = 70},
new Student(){Id = 3, Name = "赵武",Age = 14, Score = 90}
};
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Score { get; set; }
}
以上,OrderBy返回的类型是IEnumerable<Student>。
如果想使用List<T>的Sort方法,就需要让Student实现IComparable<Student>接口。
class Program
{
static void Main(string[] args)
{
List<Student> result = GetStudents();
result.Sort();
foreach (var item in result)
{
Console.WriteLine(item.Name + "--" + item.Score);
}
Console.ReadKey();
}
private static List<Student> GetStudents()
{
return new List<Student>()
{
new Student(){Id = 1, Name = "张三",Age = 15, Score = 80},
new Student(){Id = 2, Name = "李四",Age = 16, Score = 70},
new Student(){Id = 3, Name = "赵武",Age = 14, Score = 90}
};
}
}
public class Student : IComparable<Student>
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Score { get; set; }
public int CompareTo(Student other)
{
return this.Score.CompareTo(other.Score);
}
}
让Student实现IComparable<Student>接口固然很好,如果Student是一个密封类,我们无法让其实现IComparable<Student>接口呢?不用担心,Sort方法提供了一个重载,可以接收IComparer接口类型。
class Program
{
static void Main(string[] args)
{
List<Student> result = GetStudents();
result.Sort(new StudentSorter());
foreach (var item in result)
{
Console.WriteLine(item.Name + "--" + item.Score);
}
Console.ReadKey();
}
private static List<Student> GetStudents()
{
return new List<Student>()
{
new Student(){Id = 1, Name = "张三",Age = 15, Score = 80},
new Student(){Id = 2, Name = "李四",Age = 16, Score = 70},
new Student(){Id = 3, Name = "赵武",Age = 14, Score = 90}
};
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Score { get; set; }
}
public class StudentSorter : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.Score.CompareTo(y.Score);
}
}
综上,如果我们想对一个集合排序,大致有三种方式:
1、使用OrderBy方法,返回IEnumerable<T>类型。
2、让集合元素实现IComparable<T>接口,再使用Sort方法,返回void。
3、集合元素不实现IComparable<T>接口,针对集合元素类型写一个实现IComparer<T>接口的类,把该类实例作为Sort方法的参数。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对得得之家的支持。如果你想了解更多相关内容请查看下面相关链接
本文标题为:C#中对集合排序的三种方式


基础教程推荐
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中的Linq to JSON操作详解 2023-06-08
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity shader实现高斯模糊效果 2023-01-16
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C# 解析XML和反序列化的示例 2023-04-14
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30