this is a later answer, but something simple that could work for others and worked during my testing to implement something like this
var orderedList = animalList.OrderBy(x=> x.GetType().Name); foreach(var animal in orderedList) { System.Console.WriteLine(animal.Name); }
the output will look like this:
Ape Ape Ape Ape Cat Cat Cat Cat Cat Dog Dog Dog Dog Dog
where animalList looks something like this:
animalList = new List<IAnimal>();
and IAnimal interface / implementations:
public class Ape : IAnimal { public string Name => "Ape"; public AnimalType AnimalType => AnimalType.Ape; } public class Dog : IAnimal { public string Name => "Dog"; public AnimalType AnimalType => AnimalType.Dog; } public class Cat : IAnimal { public string Name => "Cat"; public AnimalType AnimalType => AnimalType.Cat; } public interface IAnimal { string Name {get;} AnimalType AnimalType {get;} }
furthermore, something like specifying an enum type that represents your class order may help. Example:
public enum MyOrderType { Ape = 1, Cat = 2, Dog = 3 }
then it's as simple as
var orderedList = animalList.OrderBy(x=> x.AnimalType).ToList();
lachlan.p.jordan
source share