C#List的排序和简单去重总结

时间:2021-12-23 07:27:50

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))

界面:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAi0AAAF3CAIAAADmduwpAAAgAElEQVR4nO3da3Bc5Zng8ffTJpvd1GRntQzD8mFKNWXIDnwIhqq5bDJVCMhibmFUY24D5qoMTILlQFuBEJuQiiwwtiSCB2wwFuGScAkJMxDSNthtbMAGgmkbjDEWtqXGlg8yPqglpaolov3Q0umj95zz9qNW67zu4/+vnlKdPv2eS3ep9E/LSaSGAQCwR2mPTwcAYIadeOKJxx9/vN6h008//cwzz0yn0729vWMAAMyY0dHRjw71/++GOXXHHae8CLW3t+dyuQdfeJdhGIZhYpjt7h+f2rlvvENnnnlmLpdb+UKWYRiGYWKbjZ8NqeKHoXQ6vfLFLMMwDMPEPOMd6u3tXfW77QzDMAwT84x36I9//ONDL+1gGIZhmJhnvENjY2Orf/8ewzAMw8Q8pQ49kn6PYRiGYWKeUofWrN3JMAzDMDFPqUOPrvuAYRiGqWw6n978vTtXXjm/rbrzvTtXdj69ObZLdD358pXfvKChrr66c+U3L+h68uWot07UoYyY9W8FhmEY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75Lpua2trKpVqRnKlUqnW1lb5twQdSiy7/ykbtcX2dyuOaXQIAGATHQIA2ESHAAA20SEAgE10CABgEx0CANhEhwAANtEhAIBNdAgAYBMdAgDYRIcAADbRIQCATXQIAGATHQIA2ESHAAA20SEAgE10CABgEx0CANhEhwAANpk6lM46DMMwDBPz0CGGYRjG5tAhhmEYxubQIYZhGMbm0CGGYRjG5tAhhmEYxuaMd+jkbTmGYSoYNZn1+2GYmpvxDgGozNhktm8HqD3/H5axXqKKUf8UAAAAAElFTkSuQmCC" alt="" />

自己定义的比较器类:

 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 ;
}
else
{
return -;
}
}
}

代码:

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 = ; i < ; i++)
{
this.dataList.Add(rm.Next(, ));
}
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(,,compare);
this.richTextBox4.Text = string.Join(",", this.dataList.ToArray());
}
}
}

演示:

C#List的排序和简单去重总结

源代码下载