贷款类网站怎样做,wordpress反向代理,赣州章贡区医保局电话,网站怎么做才能赚钱吗C#开发-集合使用和技巧#xff08;八#xff09;集合中的排序Sort、OrderBy、OrderByDescending ListT.Sort()方法签名使用场景示例升序实现效果 降序实现效果 IEnumerableT.OrderBy()方法签名使用场景示例实现效果 EnumerableT.OrderByDescending()… C#开发-集合使用和技巧八集合中的排序Sort、OrderBy、OrderByDescending ListT.Sort()方法签名使用场景示例升序实现效果 降序实现效果 IEnumerableT.OrderBy()方法签名使用场景示例实现效果 EnumerableT.OrderByDescending()使用场景示例实现效果 总结 在C#中ListT 类提供了多种方法来进行排序最常用的是 Sort 方法和IEnumerableT中提供的扩展方法 OrderBy/OrderByDescending 方法。这些方法可以按照特定的顺序重新排列列表中的元素。
List.Sort()
Sort 方法会对列表中的元素进行原地排序改变原始列表的顺序。
方法签名
public void Sort();
public void Sort(IComparerT comparer);
public void Sort(ComparisonT comparison);使用场景
当你想要直接修改现有列表的顺序时。
示例
升序
using System;
using System.Collections.Generic;public class Student
{public string Name { get; set; }public int Age { get; set; }
}public class Program
{public static void Main(){ListStudent students new ListStudent{new Student { Name 张三, Age 20 },new Student { Name 李四, Age 18 },new Student { Name 王五, Age 22 }};// 使用 Sort 方法按年龄排序students.Sort((x, y) x.Age.CompareTo(y.Age));// 输出排序后的学生名单foreach (var student in students){Console.WriteLine(${student.Name}, {student.Age});}}
}在这个例子中Sort 方法根据学生的年龄对学生列表进行了升序排序。
实现效果 降序
//降序students.Sort((x, y) -x.Age.CompareTo(y.Age));//或students.Sort((x, y) y.Age.CompareTo(x.Age));
实现效果 IEnumerable.OrderBy()
OrderBy 是 LINQ 提供的方法它可以创建一个排好序的新列表不会改变原始列表的顺序。
方法签名
public static IOrderedEnumerableTSource OrderByTSource, TKey(this IEnumerableTSource source, FuncTSource, TKey keySelector);使用场景
当你不希望改变原始列表的顺序而是想得到一个新的有序列表时。
示例
using System;
using System.Collections.Generic;
using System.Linq;public class Student
{public string Name { get; set; }public int Age { get; set; }
}public class Program
{public static void Main(){ListStudent students new ListStudent{new Student { Name 张三, Age 20 },new Student { Name 李四, Age 18 },new Student { Name 王五, Age 22 }};// 使用 OrderBy 方法按年龄排序var sortedStudents students.OrderBy(s s.Age);// 输出排序后的学生名单foreach (var student in sortedStudents){Console.WriteLine(${student.Name}, {student.Age});}}
}在这个例子中OrderBy 方法根据学生的年龄创建了一个新的有序列表原始列表 students 保持不变。
实现效果 Enumerable.OrderByDescending()
OrderByDescending 也是 LINQ 提供的方法类似于 OrderBy但是它是按照降序排序。
使用场景
当你需要按降序排序时。
示例
using System;
using System.Collections.Generic;
using System.Linq;public class Student
{public string Name { get; set; }public int Age { get; set; }
}public class Program
{public static void Main(){ListStudent students new ListStudent{new Student { Name 张三, Age 20 },new Student { Name 李四, Age 18 },new Student { Name 王五, Age 22 }};// 使用 OrderByDescending 方法按年龄降序排序var sortedStudentsDesc students.OrderByDescending(s s.Age);// 输出排序后的学生名单foreach (var student in sortedStudentsDesc){Console.WriteLine(${student.Name}, {student.Age});}}
}在这个例子中OrderByDescending 方法根据学生的年龄创建了一个新的降序列表原始列表 students 保持不变。
实现效果 总结
总结来说如果你想要改变原始列表的顺序使用 Sort如果你不想改变原始列表的顺序而是想得到一个新的有序列表使用 OrderBy 或 OrderByDescending。 文章转载自: http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.kbfzp.cn.gov.cn.kbfzp.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.knjj.cn.gov.cn.knjj.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.kysport1102.cn.gov.cn.kysport1102.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.wfhnz.cn.gov.cn.wfhnz.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.rgsnk.cn.gov.cn.rgsnk.cn http://www.morning.rchsr.cn.gov.cn.rchsr.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.slqzb.cn.gov.cn.slqzb.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.wanjia-sd.com.gov.cn.wanjia-sd.com http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.pkrb.cn.gov.cn.pkrb.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.dkfrd.cn.gov.cn.dkfrd.cn http://www.morning.fkwp.cn.gov.cn.fkwp.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.lrybz.cn.gov.cn.lrybz.cn