1

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?

3
  • possible duplicate of How do I sort a two-dimensional array in C#? Commented Apr 21, 2015 at 9:34
  • @AlexShesterov That question only seems to include sorting by one column at a time, I need to sort by two at once. Commented Apr 21, 2015 at 9:46
  • If you want to use primitive int array, write custom sort procedure ! en.wikipedia.org/wiki/Sorting_algorithm. It is a school homework Commented Apr 21, 2015 at 9:49

3 Answers 3

3

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

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.