Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a SortedDictionary based on value, not key

Tags:

c#

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 to sort my SortedDictionary based on the InTime datetime in my class. So that when I do a foreach to loop through my SortedDictionary I will have them sorted based on datetime.

Is this possible? How can I achieve it?

enter code here
 class Busdetail
    {
        public int BusNo { get; set; }
        public DateTime InTime { get; set; }
        public DateTime OutTime { get; set; }
    }
like image 458
Hukmchand Avatar asked Aug 05 '10 17:08

Hukmchand


People also ask

How do you sort SortedDictionary?

We can use the OrderBy method to sort a SortedDictionary items. The OrderBy method takes a key name that items will be sorted based on.

Can I sort a dictionary in python?

To sort a dictionary by value in Python you can use the sorted() function. Python's sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method. sorted() takes three arguments: object, key, and reverse. Dictionaries are unordered data structures.

Can dictionary be sorted C#?

How to Sort a Dictionary by Value using C#. The example code first creates a dictionary and then uses OrderBy method to sort the items. The following code snippet sorts a Dictionary by values. The code first creates a dictionary and then uses OrderBy method to sort the items.


4 Answers

Sorted Dictionary will always sort on the key, so there is no way to re-arrange it's data so they are sorted on anything other that the key. What you can do is get the data into another structure (some kind of IOrderedEnumerable) where they can be sorted on things other that the key.

If you want to discard the keys and just get the values then

var sortedValues = dictionary.Values.OrderBy(busDetail => busDetail.InTime);

will work, and the type of sortedValues will be IOrderedEnumerable<BusDetail>. If you still need to keep both the keys and values, you could do:

var sortedElements = dictionary.OrderBy(kvp => kvp.Value.InTime);

which will return a IOrderedEnumerable<KeyValuePair<int, BusDetail>>. You can that foreach on any of these two collections, or you could bind them to a grid's datasource.

like image 161
SWeko Avatar answered Oct 01 '22 22:10

SWeko


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<KeyValuePair<int, BusDetail>> 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

like image 27
Patches Avatar answered Oct 03 '22 22:10

Patches


I think you can use SortedSet and Tuple of your key and value, like: SortedSet> ((a,b)=>(a.Item2.CompareTo(b.Item2));

like image 27
user3413823 Avatar answered Sep 30 '22 22:09

user3413823


You could use a Linq query.

var D = new SortedDictionary<int, string>();
var qD = from kvp in D
         orderby kvp.Value
         select kvp
;
like image 23
Lehonti Avatar answered Oct 02 '22 22:10

Lehonti