C# SortedSet and equality

前端 未结 3 1902
清酒与你
清酒与你 2021-01-04 06:48

I am a bit puzzled about the behaviour of SortedSet, see following example:

public class Blah
{
    public double Value { get; private set; }

    public Bla         


        
3条回答
  •  离开以前
    2021-01-04 07:00

    Description

    SortedSet: You have many elements you need to store, and you want to store them in a sorted order and also eliminate all duplicates from the data structure. The SortedSet type, which is part of the System.Collections.Generic namespace in the C# language and .NET Framework, provides this functionality.

    According to MSDN Compare method returns

    • Less than zero if x is less than y.
    • Zero if x equals y.
    • Greater than zero if x is greater than y.

    More Information

    • Dotnetperls - C# SortedSet Examples
    • MSDN: Compare Method

    Update

    If your Bla class implements IComparable and you want your list sorted you can do this.

    var blahs = new List {new Blah(1), new Blah(2), 
                                new Blah(3), new Blah(2)};
    blahs.Sort();
    

    If your Bla class NOT implements IComparable and you want your list sorted you can use Linq (System.Linq namespace) for that.

    blahs = blahs.OrderBy(x => x.MyProperty).ToList();
    

提交回复
热议问题