C#List的排序和简单去重总结
List集合在开发过程中很常见,经常我们要对该集合进行一系列操作,本文介绍如何将该集合内的元素进行排序,博主制作简单WinForm应用程序进行演示。
首先,我们来看一下c#泛型List提供的Sort方法:
这里有泛型List类的Sort方法的三种形式,分别是
1,不带有任何参数的Sort方法----Sort();
这种排序List中的元素必须继承IComparable接口,并且要实现IComparable接口中的CompareTo()方法,在CompareTo()方法中要实现比较规则。
Int32和Double都是实现了IComparable接口并重载了CompareTo方法的结构。因此List<int>就可以直接进行排序。
2,带有比较器参数的Sort方法 ----Sort(IComparer<T>)
此种排序方法必须另外写一个额外的比较器类,该类必须实现IComparer接口,因为该接口有用于比较的重载函数Compare,所以我们必须实现它来完成自己希望的比较,例如自己定义一个学生类Student,改类中有ID、姓名、年龄等属性,我们可以选择年龄属性作为排序属性
3,带有比较器参数,可以指定排序范围的Sort方法----Sort(Int32, Int32 IComparer(T))
界面:
自己定义的比较器类:
public class IntCompare:IComparer<int> { /// <summary> /// 自定义比较规则(从大到小) /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public int Compare(int x, int y) { //假设原来x位于y的前面,由于x<y 索引值变为1,即索引发生了改变所以推理此比较器是从大到小 if (x < y) { return 1; } else { return -1; } } }
代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication30 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 创建全局数据集合,用来进行一系列比较 /// </summary> List<int> dataList = new List<int>(); /// <summary> /// 窗体加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { this.GetData(); } /// <summary> /// 构造测试数据 /// </summary> /// <returns></returns> private void GetData() { dataList.Clear(); Random rm = new Random(); for (int i = 0; i < 7; i++) { this.dataList.Add(rm.Next(0, 20)); } this.richTextBox1.Text = string.Join(",", this.dataList.ToArray()); } /// <summary> /// 刷新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRefresh_Click(object sender, EventArgs e) { this.richTextBox2.Clear(); this.richTextBox4.Clear(); this.richTextBox3.Clear(); this.GetData(); } /// <summary> /// 默认排序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDefault_Click(object sender, EventArgs e) { this.dataList.Sort(); this.richTextBox2.Text = string.Join(",", this.dataList.ToArray()); } /// <summary> /// 自定义排序(从大到小) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCustom_Click(object sender, EventArgs e) { IntCompare compare = new IntCompare(); this.dataList.Sort(compare); this.richTextBox3.Text = string.Join(",", this.dataList.ToArray()); } /// <summary> /// 去重 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDistinct_Click(object sender, EventArgs e) { List<int>distinctList= this.dataList.Distinct().ToList(); this.dataList.Clear(); this.dataList.AddRange(distinctList); this.richTextBox1.Text = string.Join(",", this.dataList.ToArray()); } /// <summary> /// 自定义排序部分排序(从大到小) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { IntCompare compare = new IntCompare(); //只对索引3开始的2个包括自己进行排序 this.dataList.Sort(3,2,compare); this.richTextBox4.Text = string.Join(",", this.dataList.ToArray()); } } }
演示: