Sorting an array indexed C #

Two methods are conceivable to achieve, respectively, using the list.sort () and Dictionary.OrderBy () method, as follows:

 1            int[] arrInt = new int[] { 11, 38, 12, 9, 234, 24, 441, 24, 45, 35 };
 2 
 3             //List.Sort()
 4             List<int> lstOrg = new List<int>(), lstSort = new List<int>();
 5             lstOrg.AddRange(arrInt);
 6             lstSort.AddRange(arrInt);
 7             lstSort.Sort();
 8 
 9             List<int> lstIndex = new List<int>();
10             for (int i = 0; i < lstSort.Count; i++)
11             {
12                 int index = lstOrg.IndexOf(lstSort[i]);
13                 while (lstIndex.IndexOf(index) >= 0)
14                 {
15                     index = lstOrg.IndexOf(lstSort[i], index + 1);
16                 }
17                 lstIndex.Add(index);
18                 Console.Write("({0},{1})", index, lstSort[i]);
19             }
34             Console.ReadLine();    
 1             int[] arrInt = new int[] { 11, 38, 12, 9, 234, 24, 441, 24, 45, 35 };
 2 
 3             //Dictionary.OrderBy()
 4             Dictionary<int, int> dic = new Dictionary<int, int>();
 5             for (int i = 0; i < arrInt.Length; i++)
 6             {
 7                 dic.Add(i, arrInt[i]);
 8             }
 9             dic = dic.OrderBy(o => o.Value).ToDictionary(p => p.Key, o => o.Value);
10             foreach (var item in dic)
11             {
12                 Console.Write("({0},{1})", item.Key, item.Value);
13             }
14 
15             Console.ReadLine();

Output is normal!

Always think there should be a very convenient way to achieve, but since no idea. . .

Guess you like

Origin www.cnblogs.com/fuhao/p/11412873.html