2017-05-08 75 views
-10

我有一個數組序列20,40,60,10,30,50。我想在C#中按照以下順序排列這個序列60,40,50,20,30,10。在C#中排列數組問題

任何幫助?謝謝advance☺

+3

你嘗試過什麼? –

+5

人們需要注意的是,所需的輸出不是按降序排列。這是一個他沒有給出所有要求的測試問題。 –

+0

可能的重複[如何使我的快速排序算法按升序和降序排列數組?](http://stackoverflow.com/questions/43803531/how-can-i-make-my-quick-sort -algorithm-sort-the-arrays-in-both-ascending-des) – JNW

回答

-1

你可以試試這個

int[] array = new int[] { 20, 40, 60, 10, 30, 50 }; 
    Array.Sort<int>(array, 
        new Comparison<int>((element1, element2) => element1.CompareTo(element2))); 

反向排序

element2.CompareTo(element1) 
+0

你可以簡要解釋 –

+0

MSDN鏈接:https://msdn.microsoft.com/en-us/library/aw9s5t8f(v = vs .110).aspx –

+0

是的,非常感謝它正在工作 –

0

只需使用LINQ的OrderByDescending

var list = new[] {20, 40, 60, 10, 30, 50}; 
var newList = list.OrderByDescending(x => x); 
Console.WriteLine(string.Join(",", newList)); //60,50,40,30,20,10