using System; namespace PX; public class PXTest { public static void Show() { ScoreInfo scoreInfo = new ScoreInfo() { ID = 1, Name = "张三", CSharp = 12, DataStruct = 24, DataBase = 36 }; ScoreInfo scoreInfo2 = new ScoreInfo() { ID = 2, Name = "李四", CSharp = 13, DataStruct = 24, DataBase = 36 }; ScoreInfo scoreInfo3 = new ScoreInfo() { ID = 3, Name = "王五", CSharp = 14, DataStruct = 33, DataBase = 32 }; ScoreInfo[] scoreInfos = { scoreInfo, scoreInfo2, scoreInfo3 };System.Console.WriteLine($"二分查找结果:{cs.ToString()}");
var cs = Array.BinarySearch<ScoreInfo>(scoreInfos,scoreInfo3);//返回索引
Array.Sort<ScoreInfo>(scoreInfos); foreach (var item in scoreInfos) { System.Console.WriteLine(item.ToString()); } CSharpCompare csharp = new CSharpCompare(); Array.Sort(scoreInfos, csharp); foreach (var item in scoreInfos) { System.Console.WriteLine(item.ToString()); } } } public class ScoreInfo : IComparable<ScoreInfo> { public int ID { get; set; } public string Name { get; set; } private float _cSharp; public float CSharp { get { return _cSharp; } set { _cSharp = CheckScore(value); } } public float DataStruct { get; set; } public float DataBase { get; set; } public float Total { get { return CSharp + DataStruct + DataBase; } } private float CheckScore(float score) { if (score < 0 || score > 100) { throw new ArgumentOutOfRangeException("成绩不合法!"); } return score; } public int CompareTo(ScoreInfo? other) { return ID.CompareTo(other?.ID); } public override string ToString() { return $"{ID}-{Name}-{CSharp}-{DataStruct}-{DataBase}"; } } //针对C#程序设计成绩的ICompare实现 public class CSharpCompare : IComparer<ScoreInfo> { public int Compare(ScoreInfo left, ScoreInfo right) { return right.CSharp.CompareTo(left.CSharp); } }
打印结果:
1-张三-12-24-36 2-李四-13-24-36 3-王五-14-33-32 3-王五-14-33-32 2-李四-13-24-36 1-张三-12-24-36
标签:DataBase,ScoreInfo,C#,float,ID,一个,CSharp,排序,public From: https://www.cnblogs.com/Insist-Y/p/17279693.html