Three generic sorting methods

Source: Internet
Author: User
Third of the three generic sorting methods: Use the Sort sorting method of the IComparer subclass to add to favorites
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 them, however, you must manually specify how to sort the codes;
3. List <T>. sort (IComparer <T>) uses classes that implement the IComparer <T> interface to Sort the set. You can flexibly specify how to Sort the set, 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;
}
}

Next, define a class for comparison to implement the IComparer <T> generic interface:
Public class StudentComparer: IComparer <Student>
{
Public enum CompareType
{
Name,
Age,
Grade
}

Private CompareType type;
// Constructor, based on the value of type, determines which field to sort
Public StudentComparer (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 ("James", 7, ""));
Arr. Add (new Student ("Li Si", 11, ""));
Arr. Add (new Student ("Wang Wu", 21, "First Grade "));
Arr. Add (new Student ("Chen Liu", 8, "Grade 3 "));
Arr. Add (new Student ("Liu Qi", 15, ""));

// Call the Sort method to Sort by grade
Arr. Sort (new StudentComparer (StudentComparer. CompareType. Grade ));
// Cyclically display the elements in the Set
Foreach (Student item in arr)
Console. WriteLine (item. ToString ());

// Call the Sort method to Sort by name
Arr. Sort (new StudentComparer (StudentComparer. CompareType. Name ));
// Cyclically display the elements in the Set
Foreach (Student item in arr)
Console. WriteLine (item. ToString ());
}
}

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;
}
}

Next, define a class for comparison to implement the IComparer <T> generic interface:
Public class StudentComparer: IComparer <Student>
{
Public enum CompareType
{
Name,
Age,
Grade
}

Private CompareType type;
// Constructor, based on the value of type, determines which field to sort
Public StudentComparer (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 ("James", 7, ""));
Arr. Add (new Student ("Li Si", 11, ""));
Arr. Add (new Student ("Wang Wu", 21, "First Grade "));
Arr. Add (new Student ("Chen Liu", 8, "Grade 3 "));
Arr. Add (new Student ("Liu Qi", 15, ""));

// Call the Sort method to Sort by grade
Arr. Sort (new StudentComparer (StudentComparer. CompareType. Grade ));
// Cyclically display the elements in the Set
Foreach (Student item in arr)
Console. WriteLine (item. ToString ());

// Call the Sort method to Sort by name
Arr. Sort (new StudentComparer (StudentComparer. CompareType. Name ));
// Cyclically display the elements in the Set
Foreach (Student item in arr)
Console. WriteLine (item. ToString ());
}
}

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.