C#的List实现IComparer接口排序实例

背景

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

在这里我本身经过对分数进行以下的规则排序:less

  • 首先按照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\tmath\tenglish\t\n");
		foreach (var val in ts)
		{
			Console.WriteLine("\t\t\t\t\t" + val.Chinese + "\t" + val.Math + "\t" + val.English + "\n");
		}

		// after sort output all elements in ts
		SortScore(ts);
		Console.WriteLine("After sort, all score as follows: chinese\tmath\tenglish\t\n");
		foreach (var val in ts)
		{
			Console.WriteLine("\t\t\t\t\t" + val.Chinese + "\t" + val.Math + "\t" + val.English + "\n");
		}

	}

	public static void SortScore(List<Score> scoreList)
	{
		scoreList.Sort(new ScoreComparer());
	}
}

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

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
相关文章
相关标签/搜索