C # basic --- IComparable usage, implement List & lt; T & gt;. sort () sorting,

Source: Internet
Author: User

C # basic --- IComparable usage, implement List <T>. sort () sorting,

List <T>. sort () can sort T. For example, after List <int>. sort () is executed, the set is sorted in ascending order of int. If T is a custom Object, but we want to sort it in our own way, what should we do? In fact, we can use the IComparable interface to override the CompareTo Method for implementation. The process is as follows:

1. The first step is to declare a class Person but inherit 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. Call the sort method in the main function. Classes are sorted by names from small to large. If the names are the same, classes are sorted by age.

    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. If we do not inherit the IComparable interface, how can we implement sorting. It can be implemented using Linq. In fact, the effect is the same, but if the set of classes needs to be sorted frequently, it is recommended to use the method of inheritance interface, which can simplify the sort code and make 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();        }

 


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.