代码方面:
Node.class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TEST_All
{
public class Node : IComparable<Node>
{
public int val;
public int index;
public Node(int l, int i)
{
this.index = l;
this.val= i;
}
public int CompareTo(Node b)
{
if (val > b.val) return -1; //>时,返回1(正数)按从小到大,否则-1则大到小
else if (val == b.val) return 0;
else return 1;
}
public int CompareTo(object obj)
{
Node p = obj as Node;
if (val > p.val) return -1; //>时,返回1(正数)按从小到大,否则-1则大到小
else if (val == p.val) return 0;
else return 1;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq; //
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEST_All
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Node> []way_hash = new List<Node>[10];
private int com(Node a, Node b)
{
if (a.val > b.val) return -1;
else if (a.val == b.val) return 0;
else return 1;
}
public class Comp2 : IComparer<Node>
{
public int Compare(Node a, Node b)
{
if (a.val > b.val) return -1;
else if (a.val == b.val) return 0;
else return 1;
}
}
int allused,times=0;
private void Form1_Load(object sender, EventArgs e)
{
allused = times = 0;
way_hash[0] = new List<Node>();
for (int i = 0; i < 100000; i++)
{
way_hash[0].Add(new Node(1, 3));
way_hash[0].Add(new Node(2, 2));
way_hash[0].Add(new Node(3, 7));
way_hash[0].Add(new Node(4, 7));
way_hash[0].Add(new Node(5, 5));
way_hash[0].Add(new Node(6, 1));
}
}
int usetime = 0;
private void button1_Click(object sender, EventArgs e)
{
//way_hash[0].Clear();
usetime = 0;
timer1.Enabled = true;
Go_AI_Thread();
}
string ps = "";
private void timer1_Tick(object sender, EventArgs e)
{
usetime++;
textBox1.Text = usetime.ToString();
}
#region 线程
public static IntPtr main_whandle;
public const int WATCH_FILE = 0x502;
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
void Go_AI_Thread()
{
ThreadStart thradAll = new ThreadStart(Thread_AB);
Thread TotallThread = new Thread(thradAll);
TotallThread.IsBackground = true;
TotallThread.Start();
}
#endregion
#region 已测试区 --排序方法 放到Thread_AB() 相应部分就可以排序or测试性能了,
//way_hash[0].Sort(com);
//way_hash[0].Sort((a, b) =>
//{
// if (a.val > b.val) return -1; //>时,返回1(正数)按从小到大,否则-1则大到小
// else if (a.val == b.val) return 0;
// else return 1;
//});
//way_hash[0].Sort((a, b) => a.val>b.val?-1:a.val<b.val?1:0); //way_hash[0].Sort((a, b) => { return a.val > b.val ? -1 : a.val < b.val ? 1 : 0; });
//IEnumerable<Node> q = way_hash[0].OrderByDescending(a => a.val).ToList();
//IEnumerable<Node> q = way_hash[0].OrderBy(a => a.val);
//int g = 0;
// foreach (Node c in q)
// {
// ps += c.val.ToString() + "\n";
// g++;
// if (g == 13) break;
// }
//way_hash[0] = way_hash[0].OrderByDescending(v => v.val).ToList();
//var ii = from aa in way_hash[0]
// orderby aa.val descending
// select aa;
// way_hash[0] = ii.ToList();
//way_hash[0].Sort(new Comp2());
//way_hash[0].Sort();
//for (int i = 0; i < 13; i++) ps += way_hash[0][i].index + " " + way_hash[0][i].val + "\n";
#endregion
void Thread_AB()
{
way_hash[0].Sort((a, b) => a.val>b.val?-1:1);
ps = "index val ACed\n";
timer1.Enabled = false;
for (int i = 0; i < 13; i++) ps += way_hash[0][i].index + " " + way_hash[0][i].val + "\n";
MessageBox.Show(ps);
}
}
}
个人保留总结方面,比较随意(因为自己看得懂,简而行之):
C# 如C++结构体 排序大法+list遍历方法:
(1).1新鲜: List<Node> dist = new List<Node>();
dist.Add(new Node(1.2, 0));(这个add是结合构造函数用的)
dist[1].index = 88; dist = dist.OrderBy(i => i.index).ToList();
dist = dist.OrderByDescending(i => i.index).ToList();
(1).2-> var sortedWords =
words.OrderBy(a => a.Length)
.ThenBy(a => a, new comp_cl());
public class comp_cl : IComparer<string> {
public int Compare(string x, string y)
{ return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); }
}
IEnumerable<string> q = words.OrderBy(a => a.Length); words.OrderBy(a => a.Length).ToList();才能看到线程开动的计时器,无.tolist()则textbox里不显时间,,//用这个 IEnumerable能看到排序结果foreach (string c in q(我错了,也可以sortedWords )) ss += c.ToString() + "\n";
(2)intList.Add(2); intList.Sort(); 小到大 或者(2).2对数组Array.Sort(nums);
(3)找到了习惯用的排序自定义法 dist.Sort(Compare_func);
private int Compare_func(Node a, Node b) if (a.index > b.index) return -111; //>时,返回1(正数)按从小到大,否则-1则大到小
(3).2 ->这个新鲜(使用Comparison<T>委托): //way_hash[0].Sort((a, b) =>
//{ if (a.val > b.val) return -1;
// else if (a.val == b.val) return 0;
// else return 1;
//});
//way_hash[0].Sort((a, b) => a.val>b.val?-1:a.val<b.val?1:0); //way_hash[0].Sort((a, b) => { return a.val > b.val ? -1 : a.val < b.val ? 1 : 0; }); 这两一样
(3).3应该等价于(3).2 way_hash[0].Sort((a, b) => a.val>b.val?-1:a.val<b.val?1:0);
(4).1 public class Node : IComparable<Node>
这样就可以像C++的bool operation<(a,b){}一样把排序方法写在类中了public int CompareTo(Node b) {return b.CompareTo(b); 看起来递归似的} List1.Sort();
(4).2-> public class Node : IComparable{ public int CompareTo(object obj) { Person p = obj as Person; ...}}
Array.Sort(node);(node-->只可以一维数组,,醉了) 也可以 List1.sort();
IComparable 和 CompareTo 这两名词不可变 CompareTo同时也是比较函数..这也行..
(4).3 -> 使用IComparer<T>接口: public class Comp : IComparer<Node>
{
Compare(Node a,Node b) {}
}
调用:L.Sort(new Comp()); 或者 Array.Sort(L, new Comp()); L又是只可以一维数组..
就如类中的排序方式写到类外..
Comp 名词可改 Compare不可改
(4).4->private int com(Node a, Node b) {} way_hash[0].Sort(com);
(5)新鲜排序方法:var ii = from aa in L
orderby aa.n descending
select aa;
L = ii.ToList(); //启动排序
descending不写就默认ascending
(6)关键字 串 限制:
class Item
{
public string key;// { get; set; } int c;
}
items.Add(new Item { key = "3311" });
items.Sort((i1, i2) => String.CompareOrdinal(i1.key,i2.key));//按串从小到大排
额外福利测试:
性能测试(List<Node>):
1.way_hash.Add(new Node(1, 3));平均稍微比way_hash.Add(new Node { index = 1, val = 3 });快,但都不稳定,相同环境条件下有时候相同循环次数用时相差大。
2.600W长度的排序耗时:
(4).1->way_hash[0].Sort(); 2.4 s
(4).2->way_hash[0].Sort(); 4.4 s
(4).3->way_hash[0].Sort(new Comp2()); 2.4 s
(4).4->way_hash[0].Sort(com); 2.8 s
(3).2->way_hash[0].Sort((a, b) =>{}) 2.8~2.9
(3).3 2.9s //扩大数据后比较测三元运算符速度
(5) 2.4s
(1).1 2.3~2.5s
(1).2 几乎仅次于最快的 常用的内排 (4).1 貌似还快-就是坑爹的看不到线程测出的时间--又或者是因为显示不出或者什么的所以貌似快了点
nodes.ForEach((c)又比foreach快多了
foreach (0.45s) 貌似快过for(0.6s).. while和 do-while最慢 ((0.7s))
(3).2测试发现:三元快过if el , 嵌套三元快过if elif el