List is sorted by the specified attribute (implementing the IComparer Interface)

Source: Internet
Author: User

Problem:

There is a List <CourseList> testlist. The class CourseList has five attributes: CourseId, CourseName, CourseStatus, TeacherName, LevelName, and StudentNumber. Now we want to sort testlist by the attributes of the CourseList class.

 

Solution:

 

Define enumeration types for each attribute

public enum CompareType{    CourseId,    CourseName,    CourseStatus,    TeacherName,    LevelName,    StudentNumber}

 

Implement the IComparer Interface

public class CourseListComparer:IComparer<CourseList>{       private CompareType _compareColumn;    public CompareType CompareColumn    {        get { return _compareColumn; }        set { _compareColumn = value; }    }    public CourseListComparer(CompareType t) {        this._compareColumn = t;    }    /// <summary>    /// compare by different property    /// </summary>    /// <param name="x">CourseList x</param>    /// <param name="y">CourseList y</param>    /// <returns>int </returns>    public int Compare(CourseList x,CourseList y) {               switch (this.CompareColumn) {            case CompareType.CourseId:                return x.CourseId.CompareTo(y.CourseId);            case CompareType.CourseName:                return x.CourseName.CompareTo(y.CourseName);            case CompareType.CourseStatus:                return x.CourseStatus.CompareTo(y.CourseStatus);            case CompareType.TeacherName:                return x.TeacherName.CompareTo(y.TeacherName);            case CompareType.LevelName:                return x.LevelName.CompareTo(y.LevelName);            case CompareType.StudentNumber:                return x.StudentNumber.CompareTo(y.StudentNumber);            default: return x.CourseId.CompareTo(y.CourseId);        }    }}
Call the sort method

CourseListComparer courselistComparer = new CourseListComparer(CompareType.CourseId);testList.Sort(courselistComparer);

 

Cause:

List <> in C # has two Sort methods, one with no parameters and the other with the IComparer parameter. The method without parameters is to sort by default comparator. If there are parameters, you must define an IComparer interface.

To implement the ICompare interface, you need to implement the Compare method. The Compare method has two input values to be compared. According to the custom comparison rules, int values are returned. 0 indicates equal, 1 indicates x> y, and-1 indicates x <y.

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.