C#中sort排序相关用法介绍

 C#中,List.Sort() 不仅为我们提供了默认的排序方法,还为我们提供了4种自定义排序的方法,通过默认排序方法,我们无需重写任何Sort()方法的实现代码,就能对单参数类型的List数据进行单一规则的排序,如果通过对这些方法进行改进我们可以轻松做到对多参数、多规则的复杂排序。

下面通过程序示例介绍四种相关的方法:

1.第一种,sort自带的list排序功能,但是该方法只是适用于单个元素的list。

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class test 
{
    static void Main(string[] args)
    {

        List<int> list = new List<int>();
        list.Add(6);
        list.Add(4);
        list.Add(3);
        list.Add(5);
        //直接对数字进行排序
        list.Sort();
        foreach (var item in list)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();
    }
 }

对于多元素的list,需要用不同的排序方式对元素进行排序。

未排序:

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class test 
{
    static void Main(string[] args)
    {

        List<student> stu = new List<student>();
        stu.Add(new student("apple", 23));
        stu.Add(new student("banana",18));
        stu.Add(new student("orange",19));

        foreach (var item in stu)
        {
            Console.WriteLine(item.N
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值