zoukankan      html  css  js  c++  java
  • C#的List实现IComparer接口排序实例

    背景

    在C#中,如果想要为List 实现排序,那么需要实现一个接口IComparer接口,从而根据接口中定义的方法来进行排序。
    在这里给出关于IComparer的官方参考文档IComparer参考文档

    在这里我自己通过对分数进行如下的规则排序:

    • 首先按照chinese分数排序;
    • chinese分数相同就按照math分数排序;
    • math分数也相同的话就按照English分数排序;

    代码实现

    // 分数排序
    public class Score
    {
    	private int chinese;
    	private int math;
    	private int english;
    
    	public Score(int ch, int math, int eng)
    	{
    		chinese = ch;
    		this.math = math;
    		english = eng;
    	}
    
    	public bool IsSame(Score score)
    	{
    		if (chinese == score.chinese && english == score.english && math == score.math)
    		{
    			return true;
    		}
    		return false;
    	}
    
    	public int Chinese
    	{
    		get
    		{
    			return chinese;
    		}
    		set
    		{
    			if (value != chinese)
    				chinese = value;
    		}
    	}
    
    	public int English
    	{
    		get
    		{
    			return english;
    		}
    		set
    		{
    			if (value != english)
    				english = value;
    		}
    	}
    
    	public int Math
    	{
    		get
    		{
    			return math;
    		}
    		set
    		{
    			if (value != math)
    				math = value;
    		}
    	}
    }
    
    public class ScoreComparer : IComparer<Score>
    {
    	public int Compare(Score x, Score y)
    	{
    		if (x.IsSame(y))
    		{
    			return 0;
    		}
    
    		// compare chinese score firstly
    		if (x.Chinese > y.Chinese)
    		{
    			return 1;
    		}
    		else if (x.Chinese < y.Chinese)
    		{
    			return -1;
    		}
    
    		// if chinese score is same, we should compare math score secondly
    		if (x.Math > y.Math)
    		{
    			return 1;
    		}
    		else if (x.Math < y.Math)
    		{
    			return -1;
    		}
    
    		// if chinese and math are same, we should compare english score finally
    		if (x.English > y.English)
    		{
    			return 1;
    		}
    		else if (x.English < y.English)
    		{
    			return -1;
    		}
    
    		return 0;
    	}
    }
    
    public class TestSort
    {
    	public static void Test()
    	{
    		List<Score> ts = new List<Score>();
    
    		// init ts with some score
    		ts.Add(new Score(80, 88, 45));
    		ts.Add(new Score(80, 86, 45));
    		ts.Add(new Score(83, 85, 90));
    		ts.Add(new Score(83, 85, 40));
    		ts.Add(new Score(89, 88, 49));
    		ts.Add(new Score(76, 89, 49));
    		ts.Add(new Score(73, 88, 49));
    		ts.Add(new Score(73, 82, 49));
    
    		// before sort output all elements in ts
    		Console.WriteLine("Before sort, all score as follows: chinese	math	english	
    ");
    		foreach (var val in ts)
    		{
    			Console.WriteLine("					" + val.Chinese + "	" + val.Math + "	" + val.English + "
    ");
    		}
    
    		// after sort output all elements in ts
    		SortScore(ts);
    		Console.WriteLine("After sort, all score as follows: chinese	math	english	
    ");
    		foreach (var val in ts)
    		{
    			Console.WriteLine("					" + val.Chinese + "	" + val.Math + "	" + val.English + "
    ");
    		}
    
    	}
    
    	public static void SortScore(List<Score> scoreList)
    	{
    		scoreList.Sort(new ScoreComparer());
    	}
    }
    

    在代码中关于IComparer接口的定义中实现有返回值的确定,在这我给出官方的接口定义代码,从注释中可以知道返回值代表什么意义:

    namespace System.Collections.Generic
    {
        //
        // Summary:
        //     Defines a method that a type implements to compare two objects.
        //
        // Type parameters:
        //   T:
        //     The type of objects to compare.
        public interface IComparer<in T>
        {
            //
            // Summary:
            //     Compares two objects and returns a value indicating whether one is less than,
            //     equal to, or greater than the other.
            //
            // Parameters:
            //   x:
            //     The first object to compare.
            //
            //   y:
            //     The second object to compare.
            //
            // Returns:
            //     A signed integer that indicates the relative values of x and y, as shown in the
            //     following table.Value Meaning Less than zero x is less than y.Zero x equals y.Greater
            //     than zero x is greater than y.
            int Compare(T x, T y);
        }
    }
    

    输出结果

    Before sort, all score as follows: chinese      math    english
    
                                            80      88      45
    
                                            80      86      45
    
                                            83      85      90
    
                                            83      85      40
    
                                            89      88      49
    
                                            76      89      49
    
                                            73      88      49
    
                                            73      82      49
    
    After sort, all score as follows: chinese       math    english
    
                                            73      82      49
    
                                            73      88      49
    
                                            76      89      49
    
                                            80      86      45
    
                                            80      88      45
    
                                            83      85      40
    
                                            83      85      90
    
                                            89      88      49
    
  • 相关阅读:
    弹性盒子
    bzoj4237 稻草人
    bzoj2654 tree
    bzoj4813 [Cqoi2017]小Q的棋盘
    bzoj1014 [JSOI2008]火星人
    bzoj3242 [Noi2013]快餐店
    bzoj4025 二分图
    bzoj3237 [Ahoi2013]连通图
    bzoj3244 [Noi2013]树的计数
    bzoj2431 [HAOI2009]逆序对数列
  • 原文地址:https://www.cnblogs.com/zuixime0515/p/13357886.html
Copyright © 2011-2022 走看看