SortedSet-存储类对象时的自定义顺序 [英] SortedSet - custom order when storing a class object

查看:130
本文介绍了SortedSet-存储类对象时的自定义顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑用SortedSet替换HashSet,因为它更适合我存储的数据.

I'm looking at replacing a HashSet with a SortedSet because it is better suited to the data I'm storing.

但是,到目前为止,我所看到的所有示例都与存储简单对象(整数,字符串等)有关.

However, all the examples I've seen so far relate to storing simple objects - int's, strings etc.

我想为具有许多属性的自定义类实现此功能,但是该类还包含一个我想用作索引器"的日期.

I want to implement this for a custom class with a number of properties, however the class also includes a date that I want to use as the 'indexer'.

问题是我该如何声明要使用的集合的自定义索引器,它将覆盖默认行为?

The question is how do I go about declaring a custom indexer for the set to use which will override the default behaviour?

先谢谢了.

推荐答案

实施IComparer并将其传递给SortedSet构造函数;

Implement IComparer and pass it to the SortedSet constructor;

请参阅:

https://msdn.microsoft. com/en-us/library/dd395024%28v = vs.110%29.aspx

例如:我用这个

internal class SortedIndex
{
    public double Comparable { get; set; }
    public int Index { get; set; }
}

internal class SortedIndexComparar : IComparer<SortedIndex>
{
    public int Compare(SortedIndex x, SortedIndex y)
    {
        return x.Comparable.CompareTo(y.Comparable);
    }
}

这篇关于SortedSet-存储类对象时的自定义顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆