Sorting a SortedDictionary based on value, not key

后端 未结 4 576
猫巷女王i
猫巷女王i 2021-01-18 18:11

I have a SortedDictionary, the key is a int value and the matching value to each key is a class object. The class contains a int and a two datetime variable.

I need

4条回答
  •  星月不相逢
    2021-01-18 19:14

    SortedDictionary can't be sorted by value, though you can extract a sorted list of values, as the other answers point out.

    What you want to do is use a list of keyvaluepairs instead, then sort that, like so:

    List> busses = GetMyListOfBusses();
    busses.Sort((first, next) => { 
         return first.Value.InTime.CompareTo(next.Value.Intime); 
    });
    

    At that point, your keyvaluepair list of busses will be sorted by InTime

提交回复
热议问题