按照random隨機給出的index,進行兩兩交換,當然也存在與上一次一樣的數組結果。官方還有一種ICompare的比較器,只是打亂順序這個沒用起來,不知道該怎么搞,╮(╯_╰)╭
public static List<T> SortRandom<T>(this List<T> collection)
{
for (int i = collection.Count - 1; i > 0; i--)
{
Random rand = new Random();
int p = rand.Next(i);
var temp = collection[p];
collection[p] = collection[i];
collection[i] = temp;
}
return collection;
}
事務處理,不過這個返回結果好像不怎么對
var arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
arr.Sort(delegate(int a, int b) { return (new Random()).Next(-1, 1); });
