简体   繁体   English

具有多个键的C#SortedList。 如何使用2个键(优先级和时间)实现SortedList(或其他结构)?

[英]C# SortedList with multiple keys. How do i implement a SortedList (or other struture) using 2 keys (priority and time)?

I want to write a program that simulates a waiting list based on priority and arrival time. 我想编写一个程序,根据优先级和到达时间模拟等待列表。 Priority takes higher precedent over arrival time. 优先级在到达时间上具有较高的先例。 The list will contain priority, arrival time and name.I can manage one key priority, name or arrival time, name. 该列表将包含优先级,到达时间和名称。我可以管理一个关键的优先级,名称或到达时间,名称。 What is the best way to combine 2 keys ? 组合两个键的最佳方法是什么?

Try something like code below. 试试下面的代码。 CompareTo() method will allow you to use standard sort methods. 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);
            }
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM