C#list sort and simple de-re-summary

Source: Internet
Author: User

The list collection is common in the development process, and often we want to do a series of operations on the collection, this article describes how to sort the elements within the collection, and the blogger makes a simple WinForm application to demonstrate.

First, let's look at the sort method provided by the C # generic list:
Here are three forms of the sort method for the generic list class, respectively
1, the Sort method without any parameters----sort ();

The elements in this sort list must inherit the IComparable interface, and to implement the CompareTo () method in the IComparable interface, the comparison rules are implemented in the CompareTo () method.

Both Int32 and double are structures that implement the IComparable interface and overload the CompareTo method. Therefore, the list<int> can be sorted directly.

2, sort method with comparator parameters----sort (icomparer<t>)

This sort method must also write an additional comparer class that must implement the IComparer interface because the interface has overloaded function compare for comparison, so we have to implement it to do what we want, such as defining a student class student, which has an ID, a name in the class. , age and other attributes, we can choose the age attribute as the sort attribute

3, with comparator parameters, you can specify the sort method for the sorting range----sort (Int32, Int32 IComparer (T))

Interface:

The comparator class that you define:

  Public classintcompare:icomparer<int>    {        /// <summary>        ///Custom comparison rules (from large to small)/// </summary>        /// <param name= "x" ></param>        /// <param name= "y" ></param>        /// <returns></returns>         Public intCompare (intXinty) {//assuming that the original X is in front of y, because the x<y index value becomes 1, the index has changed so that the comparator is from large to small            if(X <y) {return 1; }            Else            {                return-1; }        }      }

Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacewindowsformsapplication30{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        /// <summary>        ///Create a global data collection for a series of comparisons/// </summary>list<int> dataList =Newlist<int>(); /// <summary>        ///Form Loading/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidForm1_Load (Objectsender, EventArgs e) {                       This.                    GetData (); }        /// <summary>        ///Constructing test Data/// </summary>        /// <returns></returns>        Private voidGetData () {datalist.clear (); Random RM=NewRandom ();  for(inti =0; I <7; i++)            {                 This. Datalist.add (rm. Next (0, -)); }             This. richTextBox1.Text =string. Join (",", This. Datalist.toarray ()); }        /// <summary>        ///Refresh/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtnrefresh_click (Objectsender, EventArgs e) {             This. Richtextbox2.clear ();  This. Richtextbox4.clear ();  This. Richtextbox3.clear ();  This.        GetData (); }        /// <summary>        ///default Sort/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtndefault_click (Objectsender, EventArgs e) {             This. Datalist.sort ();  This. Richtextbox2.text =string. Join (",", This. Datalist.toarray ()); }        /// <summary>        ///custom sorting (from big to small)/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtncustom_click (Objectsender, EventArgs e) {intcompare Compare=Newintcompare ();  This. Datalist.sort (Compare);  This. Richtextbox3.text =string. Join (",", This. Datalist.toarray ()); }        /// <summary>        ///Go heavy/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtndistinct_click (Objectsender, EventArgs e) {List<int>distinctlist= This. Datalist.distinct ().            ToList ();  This. Datalist.clear ();  This. Datalist.addrange (distinctlist);  This. richTextBox1.Text =string. Join (",", This. Datalist.toarray ()); }        /// <summary>        ///Custom Sort Partial sort (from large to small)/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidButton2_Click (Objectsender, EventArgs e) {intcompare Compare=Newintcompare (); //sort only the 2 that start with index 3 including yourself             This. Datalist.sort (3,2, Compare);  This. Richtextbox4.text =string. Join (",", This. Datalist.toarray ()); }    }}
View Code

Demonstrate:

C#list sort and simple de-re-summary

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.