I have this class structure:
List<myObject> myList = new List<myObject>();
myObject
{
List<myOtherObject> myOtherList = new List<myOtherObject>();
}
myOtherObject
{
string name;
}
What would be the best way to print all the names in myList alphabetically?
I could do this, but I suspect there is a more efficient way to achieve the same:
foreach(myObject a in myList)
{
foreach(myOtherObject b in a.myOtherList)
{
newList.Add(b.name);
}
}
//print newList alphabetically
EDIT:
I was able to change the structure so that now I only need to sort myOtherList now on the basis of its name property.
I tried doing this, but it doesn’t seem to sort:
myOtherList.OrderBy(obj=>obj.name);
Does it sort it in place, or do I need to assign it to another list?
using linq (method notation):
This flattens out the
myListofmyOtherList‘s into a flat list ofmyOtherObject‘s.