Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to Sort a huge string array.
I can sort it but only column wise (y-axis). Is there a smooth way to do this by row instead (z-axis)?

my code for sorting on columns is like this and it is dynamic on datatypes.

C#
public Program()
{
    int[][] ex_arr = new int[][] { new int[] { 32, 22,0 }, new int[] { 33, 4, 12 }, new int[] { 9,0,0 } };
   Sort<int>(ex_arr, 0);
}

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


With this code I sort on the column(Y) (32,33,9).
How can i sort on row (X) in the second array? (33,4,12).
The real array is too big to post here.
Any help is appreciated. Thanks.
Posted
Updated 10-Aug-10 1:25am
v2

Try LINQ to go through the elements of the array and sort each one ..

C#
int[][] ex_arr = new int[][] { new int[] { 32, 22, 0 }, new int[] { 33, 4, 12 }, new int[] { 9, 0, 0 } };
            var qry = from x in ex_arr
                      select Sort(x);
            int[][] sorted=  qry.ToArray<int[]>();


add this function for sorting an array ... there might be a built in function for this ...

C#
int[] Sort(int[] arr)
        {
            Array.Sort(arr);
            return arr;
        }


hope this helps
 
Share this answer
 
Comments
Per Söderlund 11-Aug-10 1:27am    
The 2d jagged array i have is in relation to each other, so if i sort the middle row i want the other rows
to move accordingly.

This is nice though. Good Job.
Per Söderlund 12-Aug-10 1:10am    
Reason for my vote of 5
Good code
My solution was to use my code in the question.
I swapped the y with the x into a new array,sorted it and inserted the sorted data into the original array. Because i can't find a way to sort by rows, only columns.
The code for it is not nice and it's not clean but i have to live with it...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900