LIST comparer c# code example

Example 1: built in methods to order a list c#

using System.Linq;
//This list contains OrderDate, OrderID, Quantity, Total properties
List<Order> objListOrder = new List<Order>(); 
//How to GetOrderList(objListOrder)?
//Answer:
//- Ascending order
List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();
//- Descending order
List<Order> SortedList = objListOrder.OrderByDescending(o=>o.OrderDate).ToList();

Example 2: list sorting c#

using System.Linq;

//sorts in acending order {1,2,3,4,5}
myList.Sort();

//Sorts in decending order {5,4,3,2,1}
myList.Sort();
myList.Reverse();

//to sort a custom list of classes you have to 
//add this inside of your class
public int CompareTo(MyClass other)//replace MyClass with your class type
{
    //replace varInClass to the variable that you would like to compare
	return this.totalScore.CompareTo(other.varInClass)
}

Example 3: i comparer for lists c#

using System.Collections.Generic;
using System.Linq;

namespace YourProject.Extensions
{
    public static class ListExtensions
    {
        public static bool SetwiseEquivalentTo<T>(this List<T> list, List<T> other)
            where T: IEquatable<T>
        {
            if (list.Except(other).Any())
                return false;
            if (other.Except(list).Any())
                return false;
            return true;
        }
    }
}