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.