【问题标题】:Sorting array in ascending order error按升序排序数组错误
【发布时间】:2014-01-09 06:29:15
【问题描述】:

我下面的代码工作正常,但我想做的是根据其OrderNum 以升序显示摘要。我尝试输入Array.Sort(order[x].OrderNum),但错误无法从 int 转换为 System.Array。关于如何排序的任何建议?非常感谢!

using System;

class ShippedOrder
{

    public static void Main()
    {
        Order[] order = new Order[5];
        int x, y;
        double grandTotal = 0;
        bool goodNum;
        for (x = 0; x < order.Length; ++x)
        {
            order[x] = new Order();
            Console.Write("Enter order number: ");
            order[x].OrderNum = Convert.ToInt32(Console.ReadLine());
            goodNum = true;
            for (y = 0; y < x; ++y)
            {
                if (order[x].Equals(order[y]))
                    goodNum = false;
            }
            while (!goodNum)
            {
                Console.Write("Sorry, the order number " + order[x].OrderNum + " is a duplicate. " + "\nPlease re-enter: ");
                order[x].OrderNum = Convert.ToInt32(Console.ReadLine());
                goodNum = true;
                for (y = 0; y > x; ++y)
                {
                    if (order[x].Equals(order[y]))
                        goodNum = false;
                }
            }
            Console.Write("Enter customer name: ");
            order[x].Customer = Console.ReadLine();
            Console.Write("Enter Quantity: ");
            order[x].Quantity = Convert.ToInt32(Console.ReadLine());
        }

        Console.WriteLine("\nSummary:\n");
        for (x = 0; x < order.Length; ++x)
        {
            Array.Sort(order[x].OrderNum); //This line is where the error is located
            Console.WriteLine(order[x].ToString());
            grandTotal += order[x].Total;
        }
        Console.WriteLine("\nTotal for all orders is Php" + grandTotal.ToString("0.00"));
        Console.Read();
    }

    public class Order
    {
        public int orderNum;
        public string cusName;
        public int quantity;
        public double total;
        public const double ItemPrice = 19.95;

        public Order() { }

        public Order(int ordNum, string cusName, int numOrdered)
        {
            OrderNum = ordNum;
            Customer = cusName;
            Quantity = numOrdered;
        }

        public int OrderNum
        {
            get { return orderNum; }
            set { orderNum = value; }
        }
        public string Customer
        {
            get { return cusName; }
            set { cusName = value; }
        }
        public int Quantity
        {
            get
            {
                return quantity;
            }
            set
            {
                quantity = value;
                total = quantity * ItemPrice + 4;
            }
        }
        public double Total
        {
            get
            {
                return total;
            }
        }
        public override string ToString()
        {
            return ("ShippedOrder " + OrderNum + " " + Customer + " " + Quantity +
                " @Php" + ItemPrice.ToString("0.00") + " each. " + "Shipping is Php4.00\n" + "    The total is Php" + Total.ToString("0.00"));
        }

        public override bool Equals(Object e)
        {
            bool equal;
            Order temp = (Order)e;
            if (OrderNum == temp.OrderNum)
                equal = true;
            else
                equal = false;
            return equal;
        }
        public override int GetHashCode()
        {
            return OrderNum;
        }
    }
}

【问题讨论】:

  • 您至少应该正确掌握基本语法。编程不是猜测语法,你可以很容易地查到。
  • -1:代码墙。将您的 sn-p 减少到重现或演示您的问题所需的最小长度,以便我们能够理解它,而不是梳理(未注释的)代码沙漠。

标签: c# arrays oop sorting


【解决方案1】:

只需使用 Linq:

order = order.OrderBy(x => x.OrderNum).ToArray();

【讨论】:

  • 把错误的行全部删掉,把新行放在循环语句之前。
【解决方案2】:

在查看this link 时,发现 Array.Sort 不会将整数作为参数。

您必须将所有数据作为 Array 对象传递。

【讨论】:

    【解决方案3】:

    尝试以下方法:

    order.Sort( delegate (Order o1, Order o2) {
        return o1.OrderNum.CompareTo(o2.OrderNum);
    });
    

    【讨论】:

    • 你可以在这里使用 lambda 表达式它会简化它 -> (o1,o2)=&gt;o1.OrderNum.CompareTo(o2.OrderNum)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多