8

I have 2D jagged array. And I want to sort it by any rows.

I've searched and found code for sorting by columns

private static void Sort<T>(T[][] data, int col) 
{ 
    Comparer<T> comparer = Comparer<T>.Default;
    Array.Sort<T[]>(data, (x,y) => comparer.Compare(x[col],y[col])); 
}

Can I adapt it for sort by any rows ?

Any help is appreciated.

Sample of my jagged array (Added)

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 10;
            int[][] capm = new int[3][];
            for (int i = 0; i <= 2; i++)
            {
                capm[i] = new int[n + 1];
            }
            Random rand = new Random();            
            for (int i = 1; i <= n; i++)
            {
                capm[1][i] = i;
            }

            for (int i = 1; i <= n; i++)
            {
                capm[2][i] = rand.Next(1, 6);
            }

            Sort(capm, 2);

            Console.ReadLine();
        }
            private static void Sort<T>(T[][] data, int col)    
            {  
                data = data.OrderBy(i => i[col]).ToArray();
            }
        }

    }

@Dani & @Martin I want my jagged array to sort by capm[2][].

stereo
  • 81
  • 1
  • 4

2 Answers2

4

The only way I can think of doing this is sorting by an array of indices:

private static void Sort<T>(T[][] data, int row) 
{
    int[] Indices = new int[data[0].Length];
    for(int i = 0; i < Indices.Length; i++)
        Indices[i] = i;

    Comparer<T> comparer = Comparer<T>.Default;
    Array.Sort(Indices, (x, y) => comparer.Compare(data[row][x], data[row][y]);

    for(int i = 0; i < data.Length; i++)
    {
        T[] OldRow = (T[])data[i].Clone();
        for(int j = 0; j < OldRow.Length; j++)
            data[i][j] = OldRow[i][Indices[j]];
    }
}
Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83
Daniel
  • 30,896
  • 18
  • 85
  • 139
  • I want to sorted an entire array by a row that selected only. Don't want to sort every row. – stereo Nov 17 '11 at 05:15
  • @stereo: You mean like sort the columns of a table based on a row? – Daniel Nov 17 '11 at 05:16
  • @ Dani: Yes, I want to sort columns of a table based on a row. – stereo Nov 17 '11 at 05:21
  • @stereo: then they all must have the same length... then why is it jagged array? – Daniel Nov 17 '11 at 05:26
  • @Dani: Same length, right. I use jagged array because I heard that it would be easy to sort if I use it. – stereo Nov 17 '11 at 05:30
  • @stereo: That is somewhat right for your case, but its better to use two dimensional array – Daniel Nov 17 '11 at 05:34
  • @Dani: How to sort if I changed it to 2D array? Could you give me an example code? – stereo Nov 17 '11 at 05:42
  • @Dani: I got an error message>> Cannot implicitly convert type 'object' to 'T[]'. An explicit conversion exists (are you missing a cast?) in " T[] OldRow = data[i].Clone(); " – stereo Nov 17 '11 at 06:03
  • @Dani: My array didn't sorted after use your method. BTW please see sample of my array at question. – stereo Nov 20 '11 at 08:56
2

Given you are using a jagged array this will sort it by the 3rd item.. but a 2D array is probably better if you want to guarantee that each row has the same number of columns... If you have an array within the array which doesn't have a 3rd column, this will fail!

private static void Sort<T>(T[][] data, int col)    
{  
    data = data.OrderBy(i => i[col]).ToArray();
}

Edit:

In order to do anything with the new data reference you either need to return it or pass the parameter by reference:

private static void Sort<T>(ref T[][] data, int col)    
{  
    data = data.OrderBy(i => i[col]).ToArray();
}

The array itself isn't sorted, a new sorted array is created

Martin Booth
  • 8,485
  • 31
  • 31
  • You'll need to give me more detail than that for me to help.. I tested it and it work fine unless I've missunderstood what you expect the result to be.. – Martin Booth Nov 18 '11 at 00:07
  • May be I not clear to give you an example. Please see question again. I've already add a sample of my jagged array. – stereo Nov 20 '11 at 08:59