How to sort a collection of objects. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to sort a collection of objects.

Hello! so I have a list of objects and I want to sort this list by then value of name in the object. ie Activity.GetActivityName() I've tried using the sort function built into the list but when I try it just tells me that it "Failed to compare two elements in the array". I've tried overloading to use the getactivityname method but it says that it can't convert activity object to string. I'd really appreciate any assistance that can be offered. Thanks!

11th Dec 2016, 12:36 AM
miketech
miketech - avatar
2 Answers
+ 2
About sort function https://msdn.microsoft.com/ru-ru/library/w56d4y5z(v=vs.110).aspx (look at examples, they are pretty understandable). Also there is LINQ OrderBy and OrderByDescending functions which allow sorting by some properties.
11th Dec 2016, 4:40 AM
Ivan G
Ivan G - avatar
0
Linq is the best for sorting. for example if you have a list of employees you can you employees.OrderBy(x => x.Name)
11th Dec 2016, 8:04 AM
Elie Boustany
Elie Boustany - avatar