C#:List陣列的排序,分組,按條件去重複
阿新 • • 發佈:2018-12-27
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