c#上机实验之题目六

题目六

分析:

       要实现的就是一个排序器。

排序器

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shangji1_06
{
    public class Chair : IComparable
    {
        public double myPrice;
        public string myVendor, myID;
        public Chair() { }
        public Chair(double price, string vendor, string sku)
        {
            myPrice = price;
            myVendor = vendor;
            myID = sku;
        }
        int IComparable.CompareTo(Object obj)
        {
            if (obj is Chair)
            {
                Chair castObj = (Chair)obj;
                int result = String.CompareOrdinal(this.myID, castObj.myID);
                if (result>0) return -1;
                else if (result<0) return 1;
                else return 0;
            }
            throw new ArgumentException("object is not a Chair");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Chair chair1 = new Chair(100, "test1", "10001");
            Chair chair2 = new Chair(100, "test2", "10002");
            Chair chair3 = new Chair(100, "test3", "10003");
            Chair chair4 = new Chair(100, "test4", "10005");
            Chair chair5 = new Chair(100, "test5", "10004");
            Chair[] chairs = new Chair[5] { chair1, chair2, chair3, chair4, chair5 };
            Array.Sort(chairs);
            Console.WriteLine("排序后,依次为:");
            foreach(Chair chair in chairs)
            {
                Console.Write(chair.myID+"  ");
            }
            Console.ReadKey();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值