如何对自定义类数组进行排序?

时间:2021-11-03 21:32:38

I have a class with 2 strings and 1 double (amount).

我有一个2字符串和1双(金额)的类。

class Donator

班捐赠者

  • string name
  • 字符串名称
  • string comment
  • 字符串评论
  • double amount
  • 双倍金额

Now I have a Array of Donators filled.
How I can sort by Amount?

现在我已经填充了一系列捐款人。我如何按金额排序?

7 个解决方案

#1


22  

If you implement IComparable<Donator> You can do it like this:

如果你实现IComparable 你可以这样做:

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 的lambda替代方案:

var donors = new List<Donator>();
//add donors
donors.Sort((a, b) => a.amount.CompareTo(b.amount));

#2


4  

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

#3


4  

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

#4


2  

You could use MyArray.OrderBy(n => n.Amount) providing you have included the System.Linq namespace.

您可以使用MyArray.OrderBy(n => n.Amount),前提是您已包含System.Linq命名空间。

#5


2  

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

#6


2  

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

#7


0  

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.

这样,您可以为所需的每种特定排序设置不同的类。您可以创建一个按名称,金额或其他方式排序。

#1


22  

If you implement IComparable<Donator> You can do it like this:

如果你实现IComparable 你可以这样做:

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 的lambda替代方案:

var donors = new List<Donator>();
//add donors
donors.Sort((a, b) => a.amount.CompareTo(b.amount));

#2


4  

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

#3


4  

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

#4


2  

You could use MyArray.OrderBy(n => n.Amount) providing you have included the System.Linq namespace.

您可以使用MyArray.OrderBy(n => n.Amount),前提是您已包含System.Linq命名空间。

#5


2  

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

#6


2  

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

#7


0  

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.

这样,您可以为所需的每种特定排序设置不同的类。您可以创建一个按名称,金额或其他方式排序。