带有多个键的C#排序列表.如何使用2个键(优先级和时间)实现SortedList(或其他结构)

本文关键字:时间 优先级 实现 结构 其他 SortedList 2个键 何使用 列表 排序 | 更新日期: 2023-09-27 18:26:44

我想写一个程序,根据优先级和到达时间模拟等待列表。优先级优先于到达时间。该列表将包含优先级、到达时间和名称。我可以管理一个关键的优先级,名称或到达时间,名称。组合两个键的最佳方式是什么?

带有多个键的C#排序列表.如何使用2个键(优先级和时间)实现SortedList(或其他结构)

尝试下面的代码。CompareTo()方法将允许您使用标准排序方法。

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

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList<PriorityTime, string> sList = new SortedList<PriorityTime, string>();
        }
    }
    public class PriorityTime : IComparable<PriorityTime>
    {
        public int priority { get; set; }
        public DateTime time { get; set; }
        public int CompareTo(PriorityTime other)
        {
            if (other.priority != this.priority)
            {
                return this.priority.CompareTo(other.priority);
            }
            else
            {
                return this.time.CompareTo(other.time);
            }
        }
    }
}