C basic tutorial IComparable usage Listless thanTgreater than. sort of sort

  • 2020-12-21 18:08:34
  • OfStack

List < T > .sort () can sort T, such as List < int > After execution of sort(), the collection is sorted by int from small to large. If T is a custom Object and we want to sort it our way, we can override the CompareTo method using the IComparable interface. The process is as follows:

1. In step 1 we declare a class Person but inherit from the IComparable interface:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestIComparable
{
    public class Person : IComparable<Person>
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int CompareTo(Person obj)
        {
            int result;
            if (this.Name == obj.Name && this.Age == obj.Age)
            {
                result = 0;
            }
            else
            {
                if (this.Name.CompareTo(obj.Name) > 0)
                {
                    result = 1;
                }
                else if (this.Name == obj.Name && this.Age > obj.Age)
                {
                    result = 1;
                }
                else
                {
                    result = -1;
                }
            }
            return result;
        }
        public override string ToString()
        {
            return this.Name + "-" + this.Age;
        }
    }
}

2. Then call the sort method in the main function. The class will be sorted by name from smallest to largest, and by age if the name is the same.


public class Program
{
    public static void Main(string[] args)
    {
        List<Person> lstPerson = new List<Person>();
        lstPerson.Add(new Person(){ Name="Bob",Age=19});
        lstPerson.Add(new Person(){ Name="Mary",Age=18});
        lstPerson.Add(new Person() { Name = "Mary", Age = 17 });
        lstPerson.Add(new Person(){ Name="Lily",Age=20});
        lstPerson.Sort();
        Console.ReadKey();
    }
}

3. How do we implement sorting if we don't inherit the IComparable interface? You can do this using Linq. The effect is exactly the same, except that if the collection of classes has to be sorted frequently, it is recommended to use the inherited interface method, which simplifies the code of sort and makes it easier to understand.


public static void Main(string[] args)
        {
            List<Person> lstPerson = new List<Person>();
            lstPerson.Add(new Person(){ Name="Bob",Age=19});
            lstPerson.Add(new Person(){ Name="Mary",Age=18});
            lstPerson.Add(new Person() { Name = "Mary", Age = 17 });
            lstPerson.Add(new Person(){ Name="Lily",Age=20});
            lstPerson.Sort((x,y) =>
            {
                int result;
                if (x.Name == y.Name && x.Age == y.Age)
                {
                    result = 0;
                }
                else
                {
                    if (x.Name.CompareTo(y.Name) > 0)
                    {
                        result = 1;
                    }
                    else if (x.Name == y.Name && x.Age > y.Age)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -1;
                    }
                }
                return result;
            });
            Console.ReadKey();
        }


Related articles: