按升序错误排序数组

时间:2014-01-09 06:29:16

标签: c# arrays oop sorting

我的下面的代码工作正常,但我想要做的是按照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;
        }
    }
}

3 个答案:

答案 0 :(得分:3)

只需使用Linq:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

尝试以下方法:

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