C#:List数组的排序,分组,按条件去重复

1.数组元素   MyPoint.cs

using System;

namespace WindowsFormsApp21 {
    class MyPoint : IComparable<MyPoint> {
        public int X {
            get;
            set;
        }
        public int Y {
            get;
            set;
        }
        public MyPoint() {

        }
        public MyPoint(int x , int y) {
            this.X = x;
            this.Y = y;
        }

        public int CompareTo(MyPoint other) {
            if (this.Y == other.Y) {
                return this.X.CompareTo(other.X);  //升序  this与other交换位置则降序
            }
            else {
                return this.Y.CompareTo(other.Y);  
            }
        }
    }
}

2.逻辑代码    Form1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp21 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        List<MyPoint> ps = new List<MyPoint>();
        private void Form1_Load(object sender, EventArgs e) {
            MyPoint p1 = new MyPoint(12,17);
            MyPoint p2 = new MyPoint(14, 15);
            MyPoint p3 = new MyPoint(13, 16);
            MyPoint p4 = new MyPoint(12, 16);
            MyPoint p5 = new MyPoint(10, 11);
            MyPoint p6 = new MyPoint(16, 16);
            MyPoint p7 = new MyPoint(10, 16);
            MyPoint p8 = new MyPoint(12, 11);
            ps.Add(p1);ps.Add(p2);ps.Add(p3);ps.Add(p4);ps.Add(p5);
            ps.Add(p6); ps.Add(p7); ps.Add(p8);
            this.textBox1.Text += "排序前list的顺序打印如下:\r\n";
            foreach (MyPoint p in ps) {
                this.textBox1.Text += p.X + "   " + p.Y +"\r\n";
            }
            SortList1();
            //SortList2();
            this.textBox1.Text += "排序后list的顺序打印如下:\r\n";
            foreach (MyPoint p in ps) {
                this.textBox1.Text += p.X + "   " + p.Y + "\r\n";
            }

            this.textBox1.Text += "分组后list的打印如下:\r\n";
            //分组
            //如果我想把y一样的输出在同一行,同一行x小的在左边
            foreach (IGrouping<int ,MyPoint> group in ps.GroupBy(point => point.Y)) {
                //foreach (MyPoint p in group.OrderBy(pp => pp.X)) {}   //group.OrderBy(pp => pp.X)不需要,因为之前已经按X升序排列了
                String[] lines = new string[20];
                for (int i=0; i < lines.Length ; i++) {
                    lines[i] = ".";
                }
                foreach (MyPoint p in group) { //存储一行的数据
                    lines[p.X] = " " + p.X + "," + p.Y + " ";
                }
                this.textBox1.Text += "\r\n"+String.Join("",lines);
            }


        }

        //该种排序不需要修改MyPoint的结构,不需要实现IComparable接口也可以
        private void SortList1() {
            ps.Sort(
                delegate (MyPoint pl,MyPoint pr) {
                    if (pl.Y == pr.Y) {
                        return pl.X.CompareTo(pr.X);  //y一样则按x升序升序  pl与pr交换位置则降序
                    }
                    else {
                        return pl.Y.CompareTo(pr.Y); //默认按y升序
                    }
                }
                );
        }

        //该种排序需要修改MyPoint机构,需要实现IComparable接口
        private void SortList2() {
            ps.Sort();
        }

    }
}

3.打印如下:

排序前list的顺序打印如下:
12   17
14   15
13   16
12   16
10   11
16   16
10   16
12   11
排序后list的顺序打印如下:
10   11
12   11
14   15
10   16
12   16
13   16
16   16
12   17
分组后list的打印如下:

.......... 10,11 . 12,11 .......
.............. 14,15 .....
.......... 10,16 . 12,16  13,16 .. 16,16 ...
............ 12,17 .......

4.去重复    using System.Linq;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp22 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        List<MyPoint> ps = new List<MyPoint>();

        private void Form1_Load(object sender, EventArgs e) {
            MyPoint p1 = new MyPoint(12, 17);
            MyPoint p2 = new MyPoint(14, 15);
            MyPoint p3 = new MyPoint(13, 16);
            MyPoint p4 = new MyPoint(12, 16);
            MyPoint p5 = new MyPoint(10, 11);
            MyPoint p6 = new MyPoint(16, 16);
            MyPoint p7 = new MyPoint(10, 16);
            MyPoint p8 = new MyPoint(12, 11);
            ps.Add(p1); ps.Add(p2); ps.Add(p3); ps.Add(p4); ps.Add(p5);
            ps.Add(p6); ps.Add(p7); ps.Add(p8);
            this.textBox1.Text += "去重复前list的顺序打印如下:\r\n";
            foreach (MyPoint p in ps) {
                this.textBox1.Text += p.X + "   " + p.Y + "\r\n";
            }
            this.textBox1.Text += "去重复后list的顺序打印如下:\r\n";
            ps = ps.Distinct(new Compare<MyPoint>(
                (d1, d2) => (d1.X == d2.X)             //去除x相同的重复项,不管Y相不相同
                )).ToList();
            // ps = ps.Distinct(new Compare<MyPoint>(
            //    (d1, d2) => (d1.X == d2.X)&&(d1.Y==d2.Y)             //去除x,y相同的重复项
            //    ));
            foreach (MyPoint p in ps) {
                this.textBox1.Text += p.X + "   " + p.Y + "\r\n";
            }
        }

    }

    //去除重复通用的东西
    public delegate bool CompareDelegate<T>(T x, T y);
    public class Compare<T> : IEqualityComparer<T> {
        private CompareDelegate<T> _compare;
        public Compare(CompareDelegate<T> d) {
            this._compare = d;
        }
        public bool Equals(T x, T y) {
            if (_compare != null) {
                return this._compare(x, y);
            }
            else {
                return false;
            }
        }
        public int GetHashCode(T obj) {
            return obj.ToString().GetHashCode();
        }
    }


}

打印:

去重复前list的顺序打印如下:
12   17
14   15
13   16
12   16
10   11
16   16
10   16
12   11
去重复后list的顺序打印如下:
12   17
14   15
13   16
10   11
16   16

猜你喜欢

转载自blog.csdn.net/qq_38261174/article/details/85090452