zoukankan      html  css  js  c++  java
  • C# 数组排序带索引

    想到了两种方法来实现,分别利用了List.Sort()和Dictionary.OrderBy()方法,代码如下:

     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();

    输出正常!

    总觉得应该有很方便的方法来实现,奈何想不出来。。。

  • 相关阅读:
    安装HDP时的报错信息
    K-近邻(KNN)算法
    linux复杂命令
    azkaban报错记录
    azkaban的安装部署
    安装centOS后要解决的问题
    AI之微信跳一跳
    Python的lambda
    关于在vim中的查找和替换
    cdh6.3.2 hue集成hbase
  • 原文地址:https://www.cnblogs.com/fuhao/p/11412873.html
Copyright © 2011-2022 走看看