C# 在C中对类的数组进行排序#

C# 在C中对类的数组进行排序#,c#,C#,在我的课程中,我有这样一门课: Class Customer{ double Start; double Finish; double Wait; } 我创建了这个类的数组: Customer[] customer = new Customer[300]; 如何根据起始值降序或升序对该数组进行排序 谢谢 您可以使用该方法对数组进行就地排序: Array.Sort(customer, (x, y) => x.Start.CompareTo(y.Start));

在我的课程中,我有这样一门课:

Class Customer{

    double Start;
    double Finish;
    double Wait;
}
我创建了这个类的数组:

Customer[] customer = new Customer[300];
如何根据起始值降序或升序对该数组进行排序

谢谢

您可以使用该方法对数组进行就地排序:

Array.Sort(customer, (x, y) => x.Start.CompareTo(y.Start));
或者按降序排列

Array.Sort(customer, (x, y) => y.Start.CompareTo(x.Start));
您可以使用该方法对阵列进行适当排序:

Array.Sort(customer, (x, y) => x.Start.CompareTo(y.Start));
或者按降序排列

Array.Sort(customer, (x, y) => y.Start.CompareTo(x.Start));

您需要使用icomparer,并且在类中实现icomparer后需要编写自定义代码

如果希望使用
客户的
列表
,您需要使用icomparer,并且在类中实现icomparer后需要编写自定义代码

,但是,您可以在阵列上应用相同的函数:

List customerList=新列表;
var orderedCustomerList=customerList.OrderBy(item=>item.Start);

参考:


如果希望使用
客户的
列表
,您可以在阵列上应用相同的功能:

List customerList=新列表;
var orderedCustomerList=customerList.OrderBy(item=>item.Start);

参考:


您需要为您的客户类实现IComparable


您需要为您的客户类实现IComparable


开始的升序:

var sortedCustomers = customer.OrderBy(c => c.Start);
var sortedCustomers = customer.OrderByDescending(c => c.Start);
并按
开始
的降序排列:

var sortedCustomers = customer.OrderBy(c => c.Start);
var sortedCustomers = customer.OrderByDescending(c => c.Start);

开始的升序排列

var sortedCustomers = customer.OrderBy(c => c.Start);
var sortedCustomers = customer.OrderByDescending(c => c.Start);
并按
开始
的降序排列:

var sortedCustomers = customer.OrderBy(c => c.Start);
var sortedCustomers = customer.OrderByDescending(c => c.Start);

您也可以使用LINQ

您也可以使用LINQ


您可能需要指定LINQ是否为选项。您可能需要指定LINQ是否为选项。
OrderBy
可用于任何实现
IEnumerable
的工具,包括普通数组。尽管如此,在任何实现
IEnumerable
的东西上,都可以使用来自我的+1
OrderBy
,包括普通数组。不过,我还是给了+1