Keyser Soze
Keyser Soze

Reputation: 13

C# sorting multidimensional array by multiple columns

I have a multidimensional array that has the following values:

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

What i need to do is sort the array by the first column, and then where the values are equal, by the second column.

Ideally output should look something like this:

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

What is the best way to do this?

Upvotes: 1

Views: 5992

Answers (3)

jdweng
jdweng

Reputation: 34421

Use a List<> object with Linq

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

        }
    }
}

Upvotes: 3

Wojtek Jarzęcki
Wojtek Jarzęcki

Reputation: 15

Without Linq, sort in range columns index (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;
    }
}

You can add a third ASC / DESC parameter or a list of columns instead of a range.

use example :

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

regards WJ(AK)

Upvotes: 0

rpc1
rpc1

Reputation: 688

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]);
    }

Upvotes: 3

Related Questions