首先這是我的代碼:
int[] a = { 3, 2, 0, 4, -5, 8, 7, 6 };
Array.Sort(a);
for(int i = 0; i < a.Length; i )
{
Console.WriteLine(a[i]);
}
輸出:
-5
0
2
3
4
6
7
8
我的問題是我想像計算機一樣對所有數字進行排序,但 -5 除外。這就是我想要的輸出:
0
2
3
4
-5
6
7
8
uj5u.com熱心網友回復:
你可以這樣做:
using System.Linq;
int[] sorted = new [] { 3, 2, 0, 4, -5, 8, 7, 6 }.OrderBy(Math.Abs).ToArray();
// now you can print "sorted" as array
它將按絕對值對專案進行排序,這就是您要查找的內容。
uj5u.com熱心網友回復:
您可以使用代碼:
int[] a = { 3, 2, 0, 4, -5, 8, 7, 6 };
int[] result = a.OrderBy(a=> Math.Abs(a)).ToArray();
結果:0、2、3、4、-5、6、7、8
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/323883.html
