Three sorting methods for a set and three sorting methods for a set

Source: Internet
Author: User

Three sorting methods for a set and three sorting methods for a set

Sort the set by using the OrderBy method.

 

    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<Student> result = GetStudents().OrderBy(r => r.Score);
            foreach (var item in result)
            {
                Console.WriteLine(item.Name + "--" + item.Score);
            }
            Console.ReadKey();
        }
        private static List<Student> GetStudents()
        {
            return new List<Student>()
            {
New Student () {Id = 1, Name = "Zhang San", Age = 15, Score = 80 },
New Student () {Id = 2, Name = "", Age = 16, Score = 70 },
New Student () {Id = 3, Name = "Zhao Wu", Age = 14, Score = 90}
            };
        }
    }
    public class Student 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int Score { get; set; }
    }

 

Above, OrderBy returns IEnumerable <Student>.

 

If you want to use the Sort method of List <T>, You need to enable Student to implement the IComparable <Student> interface.

 

   class Program
    {
        static void Main(string[] args)
        {
            List<Student> result = GetStudents();
            result.Sort();
            foreach (var item in result)
            {
                Console.WriteLine(item.Name + "--" + item.Score);
            }
            Console.ReadKey();
        }
        private static List<Student> GetStudents()
        {
            return new List<Student>()
            {
New Student () {Id = 1, Name = "Zhang San", Age = 15, Score = 80 },
New Student () {Id = 2, Name = "", Age = 16, Score = 70 },
New Student () {Id = 3, Name = "Zhao Wu", Age = 14, Score = 90}
            };
        }
    }
    public class Student : IComparable<Student>
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int Score { get; set; }
        
        public int CompareTo(Student other)
        {
          return  this.Score.CompareTo(other.Score);
        }
    }

 

It is good to make Student implement the IComparable <Student> interface. If Student is a sealed class, we cannot implement the IComparable <Student> interface? Don't worry. The Sort method provides an overload that can receive IComparer interface types.

 

   class Program
    {
        static void Main(string[] args)
        {
            List<Student> result = GetStudents();
            result.Sort(new StudentSorter());
            foreach (var item in result)
            {
                Console.WriteLine(item.Name + "--" + item.Score);
            }
            Console.ReadKey();
        }
        private static List<Student> GetStudents()
        {
            return new List<Student>()
            {
New Student () {Id = 1, Name = "Zhang San", Age = 15, Score = 80 },
New Student () {Id = 2, Name = "", Age = 16, Score = 70 },
New Student () {Id = 3, Name = "Zhao Wu", Age = 14, Score = 90}
            };
        }
    }
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int Score { get; set; }
    }
    public class StudentSorter : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Score.CompareTo(y.Score);
        }
    }

 

To sum up, there are three ways to sort a set:

 

1. Use the SortBy method to return the IEnumerable <T> type.
2. Let the collection element implement the IComparable <T> interface, and then use the Sort method to return void.
3. Set elements do not implement the IComparable <T> interface. Write a class that implements the IComparer <T> interface for the set element type and use the class instance as a parameter of the Sort method.

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.