C# list的sort排序

目录

前言:

值类型的排序:

方法一:直接调用sort函数

方法二:通过C# ling表达式与CompareTo接口配合使用

方法三:降序的实现

对于自定义类型的sort排序 

方法一:通过实现IComparable接口重写CompareTo方法来排序

方法二:通过ling表达式实现 


前言:

        有时需要对List列表中内容进行排序,List 的Sort方法排序有三种结果 1,0,-1分别表示大于,等于,小于。List的sort的方法如何使用,如何实现降序和升序

值类型的排序:

方法一:直接调用sort函数

 static void Main(string[] args)
   {

            List<int> sortList = new List<int>() { 100, 101, 50, 4 };
            sortList.Sort();

            foreach (var i in sortList)
            {
                Console.Write(i);
                Console.Write(",");
            }
            sortList.Sort((a, b) => a.CompareTo(b));
            Thread.Sleep(10000);
        }

输出结果: 4,50,100,101,默认情况下,通过sort排序是升序 

方法二:通过C# ling表达式与CompareTo接口配合使用

sortList.Sort((a, b) => a.CompareTo(b)); //升序排序,与sortList.Sort()结果一样

方法三:降序的实现

可以直接在对比较结果取负值

 sortList.Sort((a, b) => -a.CompareTo(b)); //结果取负值,升序就变成了降序了

查看int32源码我们看到,int32 是实现了CompareTo接口的

public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>
  {
    internal int m_value;
    [__DynamicallyInvokable]
    public const int MaxValue = 2147483647;
    [__DynamicallyInvokable]
    public const int MinValue = -2147483648;

    public int CompareTo(object value)
    {
      if (value == null)
        return 1;
      if (!(value is int num))
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeInt32"));
      if (this < num)
        return -1;
      return this > num ? 1 : 0;
    }

    [__DynamicallyInvokable]
    public int CompareTo(int value)
    {
      if (this < value)
        return -1;
      return this > value ? 1 : 0;
    }

}

对于自定义类型的sort排序 

方法一:通过实现IComparable接口重写CompareTo方法来排序

    public class Dog : IComparable<Dog>

    {
        private float _price;

        public float price => _price;
        public Dog(float price)
        {
            _price = price;
        }

        public int CompareTo(Dog other)
        {
            if (_price > other.price)
                return 1;
            if (_price == other.price)
                return 0;
            return -1;
        }

        public override string ToString()
        {
            return _price.ToString();
        }
    }

 

        static void Main(string[] args)
        {
            List<Dog> dogs = new List<Dog>();
            dogs.Add(new Dog(12));
            dogs.Add(new Dog(10));
            dogs.Add(new Dog(14));

            dogs.Sort();

            foreach (var i in dogs)
            {
                Console.Write(i);
                Console.Write(",");
            }
            Thread.Sleep(10000);
        }

输出结果:为 10,12,14,

方法二:通过ling表达式实现 

      static void Main(string[] args)
        {
            List<Dog> dogs = new List<Dog>();
            dogs.Add(new Dog(12));
            dogs.Add(new Dog(10));
            dogs.Add(new Dog(14));

            dogs.Sort((a, b) =>
            {
                if (a.price > b.price)
                    return 1;
                if (a.price == b.price)
                    return 0;
                return -1;
            });

            foreach (var i in dogs)
            {
                Console.Write(i);
                Console.Write(",");
            }
            Thread.Sleep(10000);
        }

 

智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的些核心知识点解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头和超声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家和地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“橙点同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值