简体   繁体   English

使用对象作为SortedList的键 <Object,Value> 在C#中

[英]Using an Object as Key for a SortedList<Object,Value> in c#

How would you define an Object as Key to a SortedList in c#. 您如何将对象定义为c#中SortedList的键。

Here I would like to definte a Key Object like this 在这里我想定义一个关键对象

   MyKey key = new MyKey();
   key.type = 3; // can be 3,2 or 1
   key.time = 2014-05-03 // DateTime in c# with timestamp
   key.sequence = 5567 // a number unique to the combination above

I would like to sort this sorted list on priority type, time and sequence. 我想按优先级类型,时间和顺序对排序列表进行排序。 How do i achieve this? 我该如何实现?

Create a custom Comparer<myKey> and pass that to the SortedList constructor : 创建一个自定义的Comparer<myKey>并将其传递给SortedList构造函数

public class TypeComparer : Comparer<MyKey>
{
    public override int Compare(MyKey x, MyKey y)
    {
        if (ReferenceEquals(x, y)) return 0;
        int typeX = int.MinValue;
        int typeY = int.MinValue;
        if (x != null) typeX = x.type;
        if (y != null) typeY = y.type;
        return typeX.CompareTo(typeY);
    }
}

Now you can use this constructor: 现在您可以使用此构造函数:

var sl = new SortedList<MyKey, string>(new TypeComparer());

The SortedList in C# uses the IComparable interface for sorting the list. C#中的SortedList使用IComparable接口对列表进行排序。 So for you to achieve this u must implement the IComparable interface. 因此,为实现此目的,您必须实现IComparable接口。 See: https://msdn.microsoft.com/en-us/library/system.icomparable.compareto(v=vs.110).aspx 请参阅: https : //msdn.microsoft.com/zh-cn/library/system.icomparable.compareto(v=vs.110).aspx

A example: 一个例子:

public class Key : IComparable
{
    public int Type {get; set; }
    public DateTime Time { get; set; }
    public int Sequence { get; set; }

    int IComparable.CompareTo(object obj)
    {
        Key otherKey = obj as Key;
        if (otherKey == null) throw new ArgumentException("Object is not a Key!");

        if (Type > otherKey.Type)
            return 1;

       return -1;
    }
}

Use the sorted list: 使用排序列表:

SortedList<Key,string> collection = new SortedList<Key, string>();

collection.Add(new Key { Type = 2 }, "alpha");
collection.Add(new Key { Type = 1 }, "beta");
collection.Add(new Key { Type = 3 }, "delta");

foreach (string str in collection.Values)
{
    Console.WriteLine(str);
}

This writes: 写道:

beta 贝塔

alpha α

delta 三角洲

If I understand correctly: 如果我正确理解:

static void Main(string[] args)
{
    Dictionary<MyKey, string> fooDictionary = new Dictionary<MyKey, string>();
    fooDictionary.Add(new MyKey() {FooNumber=1, Sequence=50 }, "1");
    fooDictionary.Add(new MyKey() { FooNumber = 2, Sequence = 40 }, "2");
    fooDictionary.Add(new MyKey() { FooNumber = 3, Sequence = 30 }, "3");
    fooDictionary.Add(new MyKey() { FooNumber = 4, Sequence = 20 }, "4");
    fooDictionary.Add(new MyKey() { FooNumber = 5, Sequence = 10 }, "5");
    var result = from c in fooDictionary orderby c.Key.Sequence select c;
    Console.WriteLine("");   
}

class MyKey
{
    public int FooNumber { get; set; }
    public DateTime MyProperty { get; set; }
    public int Sequence { get; set; }
}

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

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