[英]How do I sort an array of custom classes?
I have a class with 2 strings and 1 double (amount). 我有一个带有2个字符串和1个double(数量)的类。
class Donator 类捐赠者
Now I have a Array of Donators filled. 现在,我已经填写了一系列捐赠者。
How I can sort by Amount? 如何按金额排序?
If you implement IComparable<Donator>
You can do it like this: 如果实现
IComparable<Donator>
可以这样进行:
public class Donator :IComparable<Donator>
{
public string name { get; set; }
public string comment { get; set; }
public double amount { get; set; }
public int CompareTo(Donator other)
{
return amount.CompareTo(other.amount);
}
}
You can then call sort on whatever you want, say: 然后,您可以根据需要调用sort,例如:
var donors = new List<Donator>();
//add donors
donors.Sort();
The .Sort()
calls the CompareTo()
method you implemented for sorting. .Sort()
调用您实现的CompareTo()
方法进行排序。
There's also the lambda alternative without IComparable<T>
: 还有不带
IComparable<T>
的lambda替代方案:
var donors = new List<Donator>();
//add donors
donors.Sort((a, b) => a.amount.CompareTo(b.amount));
By implementing IComparable
and then use Array.Sort
. 通过实现
IComparable
,然后使用Array.Sort
。
public class Donator : IComparable {
public string name;
public string comment;
public double amount;
public int CompareTo(object obj) {
// throws invalid cast exception if not of type Donator
Donator otherDonator = (Donator) obj;
return this.amount.CompareTo(otherDonator.amount);
}
}
Donator[] donators; // this is your array
Array.Sort(donators); // after this donators is sorted
You can also use delegates : 您也可以使用委托 :
class Program
{
static void Main(string[] args)
{
List<Donor> myDonors = new List<Donor>();
// add stuff to your myDonors list...
myDonors.Sort(delegate(Donor x, Donor y) { return x.amount.CompareTo(y.amount); });
}
}
class Donor
{
public string name;
public string comment;
public double amount;
}
I always use the list generic, for example 例如,我总是使用通用列表
List<Donator> MyList;
then I call MyList.Sort 然后我叫MyList.Sort
MyList.Sort(delegate (Donator a, Donator b) {
if (a.Amount < b.Amount) return -1;
else if (a.Amount > b.Amount) return 1;
else return 0; );
您可以使用MyArray.OrderBy(n => n.Amount)
您已包含System.Linq命名空间。
Here is a sort without having to implement an Interface. 这是一种无需实现接口的排序。 This is using a Generic List
这是使用通用列表
List<Donator> list = new List<Donator>();
Donator don = new Donator("first", "works", 98.0);
list.Add(don);
don = new Donator("first", "works", 100.0);
list.Add(don);
don = new Donator("middle", "Yay", 101.1);
list.Add(don);
don = new Donator("last", "Last one", 99.9);
list.Add(don);
list.Sort(delegate(Donator d1, Donator d2){ return d1.amount.CompareTo(d2.amount); });
Another way is to create a class that implements IComparer, then there is an overload to pass in the Comparer class. 另一种方法是创建一个实现IComparer的类,然后在Comparer类中传递一个重载。
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx
This way you could have different classes for each specific sort needed. 这样,您可以为每种所需的特定类别设置不同的类。 You could create one to sort by name, amount, or others.
您可以创建一个按名称,数量或其他排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.