为什么排序列表必须具有键值对?

时间:2021-03-20 19:27:42

If I just want a sorted list of just dates, integers, or doubles is it really necessary to have to define a SortedList(of Integer, Integer)?

如果我只想要一个只有日期,整数或双精度的排序列表,是否真的有必要定义一个SortedList(Integer,Integer)?

Seems intriguing to me, but may just be trival. I'd prefer just to use a SortedList(of Integer).

似乎很吸引我,但可能只是琐事。我更喜欢使用SortedList(Integer)。

(This question is in relation to the .Net generic collections)

(这个问题与.Net通用集合有关)

5 个解决方案

#1


8  

The next version of .NET (4.0) will have the SortedSet class that exactly does what you want. Until then, encapsulating SortedList gets closest – unless you want to implement an own class to do this, or use external collection libraries (e.g. C5 which has a SortedArray and a TreeSet class).

下一版本的.NET(4.0)将具有完全符合您要求的SortedSet类。在此之前,封装SortedList变得最接近 - 除非您想要实现自己的类来执行此操作,或者使用外部集合库(例如,具有SortedArray和TreeSet类的C5)。

#2


3  

You can use a regular List<T> and call Sort on it.

您可以使用常规List 并在其上调用Sort。

#3


1  

Yes it's necessary, because that's how the API was designed. :-)

是的,这是必要的,因为这就是API的设计方式。 :-)

But it's not hard to just make your own SortedList<T> that uses SortedList<K,V>. 5 lines of code?

但是,使用SortedList 创建自己的SortedList 并不难。 5行代码? ,v>

class SortedList<T> : IEnumerable<T> {
    SortedList<T,int> _list = new SortedList<T,int>();
    public IEnumerator<T> GetEnumerator() { return _list.Keys.GetEnumerator(); }
    IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator();  }
    public void Add(T v) { _list.Add(v, 1); }
    public int Count { get { return _list.Count; } }
}

Only problem is, SortedList can't handle dups.

唯一的问题是,SortedList无法处理重复。

#4


0  

Sorted list sorts on the key and not on the values. From MSDN

排序列表对键进行排序,而不对值进行排序。来自MSDN

The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves. In either case, a SortedList does not allow duplicate keys.

SortedList对象的元素按键排序,或者根据创建SortedList时指定的特定IComparer实现,或者根据键本身提供的IComparable实现。在任何一种情况下,SortedList都不允许重复键。

So its basically a dictionary class that supports sorting. List on the other hand sorts on values

所以它基本上是一个支持排序的字典类。另一方面,列表对值进行排序

#5


-1  

I think HashSet<int> may suit your needs.

我认为HashSet 可能适合您的需求。

#1


8  

The next version of .NET (4.0) will have the SortedSet class that exactly does what you want. Until then, encapsulating SortedList gets closest – unless you want to implement an own class to do this, or use external collection libraries (e.g. C5 which has a SortedArray and a TreeSet class).

下一版本的.NET(4.0)将具有完全符合您要求的SortedSet类。在此之前,封装SortedList变得最接近 - 除非您想要实现自己的类来执行此操作,或者使用外部集合库(例如,具有SortedArray和TreeSet类的C5)。

#2


3  

You can use a regular List<T> and call Sort on it.

您可以使用常规List 并在其上调用Sort。

#3


1  

Yes it's necessary, because that's how the API was designed. :-)

是的,这是必要的,因为这就是API的设计方式。 :-)

But it's not hard to just make your own SortedList<T> that uses SortedList<K,V>. 5 lines of code?

但是,使用SortedList 创建自己的SortedList 并不难。 5行代码? ,v>

class SortedList<T> : IEnumerable<T> {
    SortedList<T,int> _list = new SortedList<T,int>();
    public IEnumerator<T> GetEnumerator() { return _list.Keys.GetEnumerator(); }
    IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator();  }
    public void Add(T v) { _list.Add(v, 1); }
    public int Count { get { return _list.Count; } }
}

Only problem is, SortedList can't handle dups.

唯一的问题是,SortedList无法处理重复。

#4


0  

Sorted list sorts on the key and not on the values. From MSDN

排序列表对键进行排序,而不对值进行排序。来自MSDN

The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves. In either case, a SortedList does not allow duplicate keys.

SortedList对象的元素按键排序,或者根据创建SortedList时指定的特定IComparer实现,或者根据键本身提供的IComparable实现。在任何一种情况下,SortedList都不允许重复键。

So its basically a dictionary class that supports sorting. List on the other hand sorts on values

所以它基本上是一个支持排序的字典类。另一方面,列表对值进行排序

#5


-1  

I think HashSet<int> may suit your needs.

我认为HashSet 可能适合您的需求。