C#IComparable教程展示了如何在C#中使用IComparable
接口比较值。
C#IComparable接口
IComparable
接口定义了一种通用的特定于类型的比较方法,值类型或类实现该方法以对其实例进行排序或排序。
IComparable
由其值可以排序或排序的类型实现。该接口需要实现CompareTo
方法。实现的方法由Array.Sort
和List.Sort
等方法自动调用。
接口由程序员可以控制的类型使用;换句话说,通过他编写的代码。如果代码是由其他人提供的,我们将使用IComparer
接口。
C#IComparable示例
在下面的示例中,我们对员工列表进行排序。
var employees = new List<Employee> { new Employee("John Doe", 1230), new Employee("Lucy Novak", 670), new Employee("Robin Brown",2300), new Employee("Joe Draker", 1190), new Employee("Janet Doe", 980) }; employees.Sort(); employees.ForEach(employee => Console.WriteLine(employee)); Console.WriteLine("---------------------------"); employees.Reverse(); employees.ForEach(employee => Console.WriteLine(employee)); record Employee(string Name, int Salary) : IComparable<Employee> { public int CompareTo(Employee other) { // return other.Salary.CompareTo(this.Salary); if (this.Salary < other.Salary) { return 1; } else if (this.Salary > other.Salary) { return -1; } else { return 0; } } }
在示例中,我们提供了Employee
类的CompareTo
方法的实现。该类实现了IComparable
接口。
public int CompareTo(Employee other) { // return other.Salary.CompareTo(this.Salary); if (this.Salary < other.Salary) { return 1; } else if (this.Salary > other.Salary) { return -1; } else { return 0; } }
CompareTo
方法的实现将按照员工的薪水对员工进行排序。
employees.Sort();
我们对列表进行排序。该方法在排序时将已实现的CompareTo
方法考虑在内。
employees.Reverse();
我们以相反的顺序对列表进行排序。
$ dotnet run Employee { Name = Robin Brown, Salary = 2300 } Employee { Name = John Doe, Salary = 1230 } Employee { Name = Joe Draker, Salary = 1190 } Employee { Name = Janet Doe, Salary = 980 } Employee { Name = Lucy Novak, Salary = 670 } --------------------------- Employee { Name = Lucy Novak, Salary = 670 } Employee { Name = Janet Doe, Salary = 980 } Employee { Name = Joe Draker, Salary = 1190 } Employee { Name = John Doe, Salary = 1230 } Employee { Name = Robin Brown, Salary = 2300 }
C#一比较例二
在下面的示例中,我们对一组用户进行排序。
var users = new User[] { new User("Robin", "bookseller"), new User("John", "gardener"), new User("John", "writer"), new User("Janet", "teacher"), new User("Andrew", "driver"), new User("Lucy", "accountant") }; Array.Sort(users); foreach (var user in users) { Console.WriteLine(user); } Console.WriteLine("------------------------------"); Array.Reverse(users); foreach (var user in users) { Console.WriteLine(user); } record User(string Name, string Occupation) : IComparable<User> { public int CompareTo(User other) { return this.Occupation.CompareTo(other.Occupation); } }
该示例按升序和降序对用户数组进行排序。
public int CompareTo(User other) { return this.Occupation.CompareTo(other.Occupation); }
该方法按职业排序。
Array.Sort(users);
我们按升序对用户进行排序。
Array.Reverse(users);
我们按降序对用户进行排序。
$ dotnet run User { Name = Lucy, Occupation = accountant } User { Name = Robin, Occupation = bookseller } User { Name = Andrew, Occupation = driver } User { Name = John, Occupation = gardener } User { Name = Janet, Occupation = teacher } User { Name = John, Occupation = writer } ------------------------------ User { name = John, Occupation = writer } User { name = Janet, Occupation = teacher } User { name = John, Occupation = gardener } User { name = Andrew, Occupation = driver } User { name = Robin, Occupation = bookseller } User { name = Lucy, Occupation = accountant }
C#IComparable多个字段
在下面的例子中,我们通过两个字段进行比较。
var users = new List<User> { new User("Robin", "bookseller"), new User("Simon", "teacher"), new User("Arnold", "teacher"), new User("John", "gardener"), new User("Adam", "gardener"), new User("Peter", "gardener"), new User("John", "writer"), new User("Janet", "teacher"), new User("Andrew", "driver"), new User("Lucy", "accountant"), new User("Michael", "teacher") }; users.Sort(); foreach (var user in users) { Console.WriteLine(user); } record User(string Name, string Occupation) : IComparable<User> { public int CompareTo(User other) { int res = this.Occupation.CompareTo(other.Occupation); if (res == 0) { res = other.Name.CompareTo(this.Name); } return res; } }
我们有相同职业的用户。在这种情况下,我们会比较他们的名字。
public int CompareTo(User other) { int res = this.Occupation.CompareTo(other.Occupation); if (res == 0) { res = other.Name.CompareTo(this.Name); } return res; }
首先,我们根据用户的Occupation
字段比较用户。如果它们相等,我们比较它们的Name
字段。我们比较名称的方式导致降序排序。
$ dotnet run User { Name = Lucy, Occupation = accountant } User { Name = Robin, Occupation = bookseller } User { Name = Andrew, Occupation = driver } User { Name = Peter, Occupation = gardener } User { Name = John, Occupation = gardener } User { Name = Adam, Occupation = gardener } User { Name = Simon, Occupation = teacher } User { Name = Michael, Occupation = teacher } User { Name = Janet, Occupation = teacher } User { Name = Arnold, Occupation = teacher } User { Name = John, Occupation = writer }
在这篇文章中,我们使用IComparable
接口在C#中对数据进行排序。
列出所有C#教程。