【问题标题】:C# sorting multidimensional array by multiple columnsC#按多列对多维数组进行排序
【发布时间】:2015-06-28 09:03:48
【问题描述】:

我有一个具有以下值的多维数组:

multiarray = new int[,] {    { 8, 63  },
                             { 4, 2   }, 
                             { 0, -55 }, 
                             { 8, 57  }, 
                             { 2, -120}, 
                             { 8, 53  }  };

我需要做的是按第一列对数组进行排序,然后在值相等的地方按第二列排序。

理想的输出应该是这样的:

8, 63
8, 57
8, 53
4, 2
2, -120
0, -55

最好的方法是什么?

【问题讨论】:

标签: c# sorting multidimensional-array


【解决方案1】:

在 Linq 中使用 List 对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication19
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<int>> multiarray = new List<List<int>>{    
                new List<int> { 8, 63  },
                new List<int>  { 4, 2   }, 
                new List<int>  { 0, -55 }, 
                new List<int>  { 8, 57  }, 
                new List<int>  { 2, -120}, 
                new List<int>  { 8, 53  }  
            };
           

            List<List<int>> sortedList = multiarray.OrderBy(x => x[1]).OrderBy(y => y[0]).ToList();

        }
    }
}

【讨论】:

    【解决方案2】:
    using System.IO;
    using System;
    using System.Linq;
    
    class Program
    {
        static void Main()
        {
            // declare and initialize a JAGGED array
             int[][] a=new int[][]{   new int[] { 8, 63  },
                                new int[]  { 4, 2   }, 
                                new int[]  { 0, -55 }, 
                                 new int[] { 8, 53  }, 
                                 new int[] { 2, -120}, 
                                 new int[] { 8, 57  }  };
    
          //using LAMBDA expression
          Array.Sort(a, (a1, a2) => { return (a2[0]-a1[0])+((a2[0]-a1[0])==0?(a2[1]-a1[1]):0); 
          });
    
           Console.WriteLine("LAMBDA Sorting");   
          for(int i=0;i<6;i++)
             Console.WriteLine(" {0}:{1} ",a[i][0],a[i][1]);
    
          //using LINQ   
          var sorted = from x in a
                 orderby x[0] descending ,x[1]  descending
                 select x;
    
           Console.WriteLine("LINQ Sorting");      
           foreach(var item in sorted )
            Console.WriteLine(" {0}:{1} ",item[0], item[1]);
        }
    

    【讨论】:

      【解决方案3】:

      不使用 Linq,按范围列索引排序(FromIndex ... ToIndex):

          public class MyListArrComparer : IComparer<Int32[]>
      {
          public Int32 FromIndex {get; set;}
          public Int32 ToIndex {get; set;}
          public int Compare(Int32[] x, Int32[] y)
          {
            for (Int32 index=FromIndex; index<=ToIndex; index++) {
                if (x[index]>y[index]) return -1;
                if (x[index]<y[index]) return 1;
            }
            return 0;
          }
      }
      

      您可以添加第三个 ASC / DESC 参数或列列表而不是范围。

      使用示例:

      MyListArrComparer comps = new MyListArrComparer();
      comps.FromIndex=0;
      comps.ToIndex=3;
      hhList.Sort(comps);
      

      问候 WJ(AK)

      【讨论】:

        猜你喜欢
        • 2011-03-15
        • 2020-04-01
        • 2014-05-13
        • 2013-12-09
        • 2015-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多