4.排序操作符
OrderBy
作用:基于一个keySelector方法对输入序列进行排序,keySelector方法
将为每个输入元素返回一个键值,并会生成一个已排序的输出序列
IOrderedEnumerable<T>,这个输出序列将根据返回的键值按升序排列。
原型
public static IOrderedEnumerable<T> OrderBy<T, K>( this IEnumerable<T> source, Func<T, K> keySelector) where K:IComparable<K>;
public static IOrderedEnumerable<T> OrderBy<T, K>( this IEnumerable<T> source, Func<T, K> keySelector, IComparable<K> comparer);
例子
static void Main(string[] args) { int[] items = { 12,23,1,56,3,10,100,32,9,15}; var result = items.OrderBy(c => c); //显示查询结果 foreach (var item in result) { Console.WriteLine(item); } Console.Read(); }
结果
例子
public class Student { public int Id; public string Name; public int Age; } public class TestComparer : IComparer<int> { public int Compare(int x, int y) { if (x < y) { return (-1); } else if (x > y) { return (1); } else { return (0); } } }
static void Main(string[] args) { List<Student> items = new List<Student>() { new Student{ Id = 1, Name = "yxg1", Age = 20 }, new Student{ Id = 2, Name = "yxg2", Age = 10 }, new Student{ Id = 3, Name = "yxg3", Age = 60 }, new Student{ Id = 4, Name = "yxg4", Age = 50 }, new Student{ Id = 5, Name = "yxg5", Age = 70 } }; TestComparer t = new TestComparer(); var result = items.OrderBy(c => c.Age,t); //显示查询结果 foreach (var item in result) { Console.WriteLine(item.Id + ":" + item.Name + ":" + item.Age); } Console.Read(); }
结果
OrderByDescending
作用: 和OrderBy相似,区别在于对序列中的元素进行降序排列。
原型:和OrderBy操作符相同
public static IOrderedEnumerable<T> orderByDescending<T, K>( this IEnumerable<T> source, Func<T, K> keySelector) where K: IComparable<K>;
public static IOrderedEnumerable<T> OrderBy<T, K>( this IEnumerable<T> source, Func<T, K> keySelector, IComparable<K> comparer);
例子
static void Main(string[] args) { int[] items = { 12, 23, 1, 56, 3, 10, 100, 32, 9, 15 }; var result = items.OrderByDescending(c => c); //显示查询结果 foreach (var item in result) { Console.WriteLine(item); } Console.Read(); }
结果
例子
public class Student { public int Id; public string Name; public int Age; } public class TestComparer : IComparer<int> { public int Compare(int x, int y) { if (x < y) { return (-1); } else if (x > y) { return (1); } else { return (0); } } }
static void Main(string[] args) { List<Student> items = new List<Student>() { new Student{ Id = 1, Name = "yxg1", Age = 20 }, new Student{ Id = 2, Name = "yxg2", Age = 10 }, new Student{ Id = 3, Name = "yxg3", Age = 60 }, new Student{ Id = 4, Name = "yxg4", Age = 50 }, new Student{ Id = 5, Name = "yxg5", Age = 70 } }; TestComparer t = new TestComparer(); var result = items.OrderByDescending(c => c.Age,t); //显示查询结果 foreach (var item in result) { Console.WriteLine(item.Id + ":" + item.Name + ":" + item.Age); } Console.Read(); }
结果
ThenBy
作用:使用在OrderBy(OrderByDescending)操作符之后,输入序列根据keySelector方法
将返回一个键值进行排序,并将生成一个类型为IOrderedEnumerable<T>的已排序的输出序列
原型
public static IOrderedEnumerable<T> ThenBy<T, K>( this IOrderedEnumerable<T> source, Func<T, K> keySelector) where K : IComparable<K>;
public static IOrderedEnumerable<T> ThenBy<T, K>( this IOrderedEnumerable<T> source, Func<T, K> keySelector, IComparer<K> comparer);
ThenByDescending
作用:和ThenBy相似,区别在于对序列中的元素进行降序排列。
原型
public static IOrderedEnumerable<T> ThenBy<T, K>( this IOrderedEnumerable<T> source, Func<T, K> keySelector) where K : IComparable<K>;
public static IOrderedEnumerable<T> ThenBy<T, K>( this IOrderedEnumerable<T> source, Func<T, K> keySelector, IComparer<K> comparer);
以上两个操作符和之前的操作符类似,就不写例子了。
Reverse
作用:可以输出与输入序列的类型相同,但顺序相反的序列。
原型
public static IEnumerable<T> Reverse<T>( this IEnumerable<T> source );
例子
static void Main(string[] args) { int[] items = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var result = items.Reverse(); //显示查询结果 foreach (var item in result) { Console.WriteLine(item); } Console.Read(); }
结果