Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# list.sort

作者:卍…`傷`   发布日期:2026-07-11   浏览:65

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 创建一个整数列表
        List<int> numbers = new List<int> { 5, 2, 9, 1, 5, 6 };

        // 对列表进行排序
        numbers.Sort();

        // 输出排序后的列表
        Console.WriteLine("Sorted list of numbers:");
        foreach (int num in numbers)
        {
            Console.WriteLine(num);
        }

        // 创建一个字符串列表
        List<string> words = new List<string> { "apple", "orange", "banana", "grape" };

        // 对字符串列表进行排序
        words.Sort();

        // 输出排序后的字符串列表
        Console.WriteLine("\nSorted list of words:");
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
    }
}

解释说明:

  1. 创建列表:我们首先创建了两个列表,一个是整数列表 numbers,另一个是字符串列表 words
  2. 排序:使用 Sort() 方法对列表进行排序。对于整数列表,默认是按数值升序排序;对于字符串列表,默认是按字母顺序排序。
  3. 输出结果:通过 foreach 循环遍历并输出排序后的列表内容。

这段代码展示了如何使用 List<T>.Sort() 方法对不同类型的列表进行排序。

上一篇:opencv c#

下一篇:c# protected

大家都在看

c# 二进制

c# 创建目录

c# socket服务端连接多个客户端

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

c# invoke方法

.net和c#

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站