'Function To Dynamically Sort A List Of Different Objects By Their Properties/Fields

My goal is for the user to be able to sort lists of different objects by their properties / fields by name

(These lists will be presented in a table with headers that they can click to sort them)

I'm not sure how to dynamically specify the type of the property/field in this line

Expression.Lambda<Func<T, int>>(

I've got as far as the code below but obviously fails when I sort by name as it's not an int.

class Person
    {
        public int Id;
        public string Name;

        public Person(int id, string name)
        {
            Id = id;
            Name = name;
        }
    }

    class Car
    {
        public int Id;
        public DateTime Manufactured;

        public Car(int id, DateTime manufactured)
        {
            Id = id;
            Manufactured = manufactured;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Create list of people
            List<Person> listOfPeople = new List<Person>
            {
                new Person(1,"Nick"),
                new Person(2,"Barry")
            };

            //Sort people by Id
            List<Person> peopeSortedById = 
                DynamicSort(
                    propertyOrFieldName: "Id",
                    list: listOfPeople);

            //Sort people by Name
            List<Person> peopleSortedByName =
                DynamicSort(
                    propertyOrFieldName: "Name",
                    list: listOfPeople);

            //Create list of cars
            List<Car> listOfCars = new List<Car>
            {
                new Car(1,DateTime.Now),
                new Car(2,DateTime.Now)
            };

            //Sort cars by Id
            List<Car> carsSortedById =
                DynamicSort(
                    propertyOrFieldName: "Id",
                    list: listOfCars);

            //Sort cars by Name
            List<Car> carsSortedByName =
                DynamicSort(
                    propertyOrFieldName: "Name",
                    list: listOfCars);

            Console.Read();
        }

        public static List<T> DynamicSort<T>(
            string propertyOrFieldName,
            List<T> list)
        {
            var objectType =
                    Expression.Parameter(typeof(T));

            var propertyOrField =
                Expression.PropertyOrField(
                    expression: objectType,
                    propertyOrFieldName: propertyOrFieldName);

            var propertyOrFieldType =
                propertyOrField.Type;

            var expression =
                Expression.Lambda<Func<T, int>>(
                body: propertyOrField,
                parameters: objectType)
                .Compile(); ;

            return list.AsQueryable().OrderBy(expression).ToList();
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source