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

发布于 2024-08-25 12:09:56 字数 179 浏览 9 评论 0原文

我有一个包含 2 个字符串和 1 个双精度(数量)的类。

class Donator

  • string name
  • string comment
  • double amount

现在我已经填充了一个捐赠者数组。
如何按金额排序?

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

class Donator

  • string name
  • string comment
  • double amount

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你在我安 2024-09-01 12:09:56

如果您实施 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);
  }
}

然后,您可以根据需要调用排序,例如:

var donors = new List<Donator>();
//add donors
donors.Sort();

.Sort() 调用您实现的用于排序的 CompareTo() 方法。

还有不带 IComparable 的 lambda 替代方案:

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

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

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:

var donors = new List<Donator>();
//add donors
donors.Sort();

The .Sort() calls the CompareTo() method you implemented for sorting.

There's also the lambda alternative without IComparable<T>:

var donors = new List<Donator>();
//add donors
donors.Sort((a, b) => a.amount.CompareTo(b.amount));
冷情 2024-09-01 12:09:56

通过实现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

By implementing IComparable and then use 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
最舍不得你 2024-09-01 12:09:56

您还可以使用委托

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

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;
}
千紇 2024-09-01 12:09:56

我总是使用列表通用,例如

List<Donator> MyList;

然后我调用 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; );

I always use the list generic, for example

List<Donator> MyList;

then I call 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; );
无畏 2024-09-01 12:09:56

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

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

神也荒唐 2024-09-01 12:09:56

这是一种无需实现接口的排序。这是使用通用列表

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

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); });
深陷 2024-09-01 12:09:56

另一种方法是创建一个实现 IComparer 的类,然后在 Comparer 类中传递一个重载。

http://msdn.microsoft.com/en-us/library/8ehhxeaf。 aspx

这样,您就可以为所需的每种特定排序提供不同的类。您可以创建一个按名称、金额或其他排序的列表。

Another way is to create a class that implements IComparer, then there is an overload to pass in the Comparer class.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文