Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
how to order a
List<Dictionary<String,object>>
the object contains only string
the dictionary is structured as below
[name: mario]
[surname: rossi]
[address: xxxxxx]
[age: 40]
i'd like to order the list of these dictionaries by "age"
may you help me please?
i've tried with:
myList.Select(x => x.OrderBy(y=>y.Value))
it gives me this error: the value isn't a string(ok i aggree with him, it's an object, i've to cast in some way)
but more important is that i can't tell to the "orderby" methods that it must order by age
–
–
–
–
You want something along the lines of
var sorted = myList.OrderBy(dict => Convert.ToInt32(dict["age"]));
I 've used Convert.ToInt32
defensively because there's a small chance you are using another data type for the age, and casting (unboxing) to the wrong type would throw an exception.
However! It would be much easier if you used a proper type instead of the dictionary; then it would just be:
myList.Sort((x,y) => x.Age.CompareTo(y.Age));
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.