c#怎么对list里面的值重新排序

来源:前端技术作者:不吃香菜头衔:草根站长
导读:本期聚焦于小伙伴创作的《c#怎么对list里面的值重新排序》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《c#怎么对list里面的值重新排序》有用,将其分享出去将是对创作者最好的鼓励。

在c#编程中,对list里面的值重新排序是最常见的数据处理需求之一。根据数据类型和排序规则的不同,我们可以选择不同的实现方式,既可以使用List类自带的Sort方法,也可以借助LINQ提供的OrderBy等扩展方法。

c#怎么对list里面的值重新排序

使用List的Sort方法

List<T>本身提供了Sort方法,可以直接对集合进行原地排序。对于基础类型如int、string,默认会按照从小到大或从字母顺序排。

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> nums = new List<int> { 5, 2, 8, 1, 3 };
        // 默认升序排序
        nums.Sort();
        foreach (int n in nums)
        {
            Console.WriteLine(n);
        }
    }
}

使用自定义比较器降序

如果需要降序,可以传入Comparison委托或者使用Reverse方法。

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> nums = new List<int> { 5, 2, 8, 1, 3 };
        // 使用Comparison进行降序
        nums.Sort((a, b) => b.CompareTo(a));
        foreach (int n in nums)
        {
            Console.WriteLine(n);
        }
    }
}

使用LINQ的OrderBy和OrderByDescending

LINQ方式不会修改原list,而是返回一个新的有序序列,适合函数式风格编码。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> nums = new List<int> { 5, 2, 8, 1, 3 };
        // 升序
        var asc = nums.OrderBy(x => x).ToList();
        // 降序
        var desc = nums.OrderByDescending(x => x).ToList();
        asc.ForEach(x => Console.Write(x + " "));
        Console.WriteLine();
        desc.ForEach(x => Console.Write(x + " "));
    }
}

对对象列表按属性排序

当list中存放的是自定义对象时,可以指定属性作为排序键。

using System;
using System.Collections.Generic;
using System.Linq;

class Student
{
    public string Name { get; set; }
    public int Score { get; set; }
}

class Program
{
    static void Main()
    {
        List<Student> students = new List<Student>
        {
            new Student { Name = "Tom", Score = 80 },
            new Student { Name = "Amy", Score = 95 },
            new Student { Name = "Bob", Score = 70 }
        };
        // 按分数升序
        var sorted = students.OrderBy(s => s.Score).ToList();
        sorted.ForEach(s => Console.WriteLine(s.Name + ":" + s.Score));
    }
}

多条件排序

使用ThenBy可以在主排序条件相同的情况下指定次要排序条件。

using System;
using System.Collections.Generic;
using System.Linq;

class Student
{
    public string Name { get; set; }
    public int Score { get; set; }
}

class Program
{
    static void Main()
    {
        List<Student> students = new List<Student>
        {
            new Student { Name = "Tom", Score = 80 },
            new Student { Name = "Amy", Score = 80 },
            new Student { Name = "Bob", Score = 70 }
        };
        // 先按分数降序,再按名字升序
        var sorted = students
            .OrderByDescending(s => s.Score)
            .ThenBy(s => s.Name)
            .ToList();
        sorted.ForEach(s => Console.WriteLine(s.Name + ":" + s.Score));
    }
}

方法对比

方式是否修改原list适用场景
Sort方法原地排序、性能敏感
OrderBy链式查询、只读排序
自定义比较器视调用方式复杂对象排序

在实际项目中,如果只是简单调整顺序且不在意原集合变化,用Sort更直观;若需要保持原数据不变并组合其他查询,LINQ的OrderBy是更优选择。

C#List排序OrderBySort方法LINQ修改时间:2026-07-25 11:12:11

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。