700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > c#自定义类的指定字段排序

c#自定义类的指定字段排序

时间:2019-07-17 05:06:16

相关推荐

c#自定义类的指定字段排序

类Chair的定义代码如下:

public class Chair{public double myPrice;public string myVendor, myID;public Chair() {}public Chair(double price, string vendor, string sku){myPrice = price;myVendor = vendor;myID = sku;}}

要求通过调用Array.Sort方法对Chair 对象数组按myID的unicode码值进行由大到小排序。

//通过调用Array.Sort方法对Chair对象数组按myID的unicode码值进行由大到小排序using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp6{class Chair : IComparable{public double myPrice;public string myVendor, myID;public Chair() {}public Chair(double price, string vendor, string id){myPrice = price;myVendor = vendor;myID = id;}int pareTo(Object obj){if (obj is Chair){Chair castObj = (Chair)obj;if ((pareOrdinal(this.myID,castObj.myID) < 0)) return 1;else if ((pareOrdinal(this.myID, castObj.myID)) > 0) return -1;else return 0;}throw new ArgumentException("object is not a Chair");}}class Program{static void Main(string[] args){Chair[] chairs = new Chair[4];chairs[0] = new Chair(150.0, "gdne", "9988");chairs[1] = new Chair(250.0, "Lan", "w");chairs[2] = new Chair(100.0, "lne", "汉");chairs[3] = new Chair(120.0, "Harris", "939");Array.Sort(chairs);foreach (Chair c in chairs){Console.WriteLine(c.myPrice + " " + c.myVendor + " " + c.myID);}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。