Three generic sorting methods: Sort sorting method of the icomparer subclass

Source: Internet
Author: User
There are three methods for sorting generics:
1 , List < T > . Sort (), which can only be used when the set element implements the icomparable generic interface
2 , List < T > . Sort (comparison < T > ), Comparison < T > Is a method delegate. It has two parameters T and returns the int type. You can flexibly specify how to sort the data, but manually specify how to sort the data when encoding is required;
3 , List < T > . Sort (icomparer < T > ). < T > The class of the interface sorts the set. You can specify how to sort the set flexibly, but you need to define the class sorting method in advance.

The third method is described here:
First, define a class as the element of the set.
Using System;
Using System. Collections. Generic;
///   <Summary>
/// Student
///   </Summary>
Public   Class Student
{
Private   String Name;
// Name
Public   String Name
{
Get { Return Name ;}
Set {Name = Value ;}
}
Private   Int Age;
// Age
Public   Int Age
{
Get { Return Age ;}
Set {Age = Value ;}
}

Private   String Grade;
// Grade
Public   String Grade
{
Get { Return Grade ;}
Set {Grade = Value ;}
}
// Constructor
Public Student ( String Name, Int Age, String Grade)
{
This . Name = Name;
This . Age = Age;
This . Grade = Grade;
}
Public   Override   String Tostring ()
{
Return   This . Name +   " , "   +   This . Age. tostring () +   " , "   +   This . Grade;
}
}

define a comparison class to implement icomparer T generic interface:
Public class studentcomparer: icomparer Student >
{< br> Public Enum comparetype
{< br> name,
Age,
Grade
}

PrivateComparetype type;
//The constructor determines which field to sort based on the value of type.
PublicStudentcomparer (comparetype type)
{
This. Type=Type;
}

# Region Icomparer <student> Member
Public   Int Compare (student X, student y)
{
Switch ( This . Type)
{
Case Comparetype. Name:
Return X. Name. compareto (Y. Name );
Case Comparetype. Age:
Return X. Age. compareto (Y. Age );
Default : // Case comparetype. grade:
Return X. Grade. compareto (Y. grade );
}
}

# Endregion
}

The sorting is started as follows:
Using System;
Using System. Collections. Generic;
Public   Class Test
{
Public   Static   Void Main ()
{
List < Student > Arr =   New List < Student > ();
Arr. Add ( New Student ( " Zhang San " , 7 , " Grade 1 " ));
Arr. Add ( New Student ( " Li Si " , 11 , " Grade 2 " ));
Arr. Add ( New Student ( " Wang Wu " , 21 , " Grade 1 " ));
Arr. Add ( New Student ( " Chen Liu " , 8 , " Grade 3 " ));
Arr. Add ( New Student ( " Liu Qi " , 15 , " Grade 2 " ));

// Call the sort method to sort by grade
Arr. Sort ( New Studentcomparer (studentcomparer. comparetype. Grade ));
// Show elements in the Set cyclically
Foreach (Student item In ARR)
Console. writeline (item. tostring ());

// Call the sort method to sort by name
Arr. Sort ( New Studentcomparer (studentcomparer. comparetype. Name ));
// Show elements in the Set cyclically
Foreach (Student item In ARR)
Console. writeline (item. tostring ());
}
}

ArticleSource: http://Www.diybl.com/course/4_webprogram/asp.net/netjs/200864/121436.html

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.