Newbie in C#, the problem about 2 dimension array descending sort.  
Author Message
Rush hour





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

Hey all,
I met the problem about 2-dimension array descending sort.
E.g. int [,] intarry = new int {{0,1,2},{0,3,4},{0,2,3,}}
I wanna have this intarry in terms of descending sort, as the 2nd item. E.g. the expected result will be intarry = {{0,3,4}, {0,2,3},{0,1,2}}
From the information that I got from webstie, the 1 dimension array can be applied by reverse method of array. However, for the multiple array, the Comparer should be written. Furthermore, there are few information availble and one even with the commerical application, e.g. you have to pay for it in order to see the possible solution. :0( I am a little new in c#. Therefore, I may need some helps from you....
According to my case, can you do kindly to provide a piece of codes or links that you are ever experienced
Thanks in advance,
:0)



Visual C#11  
 
 
Paul Louth





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

First, rather than using int[,] intarray, you should use a List<T> as it will allow you to sort the items:

   List<int[]> list = new List<int[]>();
   list.Add( new int[] {0,1,2} );
   list.Add( new int[] {0,3,4} );
   list.Add( new int[] {0,2,3} );

The first line creates a List which manages arrays of int[].  The following three lines adds the int arrays to the list.

Then you need to build a comparer-class.  The comparer inherits Comarer<T>, where T will be replaced with int[], so it's for comparing int arrays.

public class CompareMidArray : Comparer<int[]>
{
    public override int Compare(int[] x, int[] y)
    {
        return y[1].CompareTo(x[1]);
    }
}

Normally the Compare function would compare X with Y, but I'm comparing Y with X, because you want your list to be descending.

Next we sort the list using the comparer:

   list.Sort(new CompareMidArray());

Any questions feel free to shout


 
 
Luis in SE





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

Hey Paul,
I am trying the code you provided, thank you so much.
However, in my case, I had one 2-dimension array, say 1000*3, by which means that I will have one 1000 one-dimesion arrays and add them in the list once per time.
It seems a little much manual work...


 
 
Paul Louth





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

What's more work, writing your own sorting algorithm, or writing some code to populate a List
 
 
Luis in SE





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

Hey Paul,
 
My case is as follows,
 
Firstly, I have to read datas from the file A, then the expected output will be one 2-dimension array. Because there are about 1,000 records with 3 different items in the file A.
 
Then, I have to sort the array as you proposed. In this sense, I will have 1,000 one-dimension array instead of one 2-dimension array with 1,000 rows. So, there are some more works about such 1,000 one-dimension arraya and add them to the list...
 
Or I am wrong...


 
 
Paul Louth





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

They're the same. A 1000x3 2d array can be though of as 1000 1d arrays with each 1d array having a length of 3.

So to have a list with 1000 items in, each with 3 entries, is exactly the same as a 1000x3 2D array. Only slightly more flexible.


 
 
Luis in SE





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

Hey paul,
 
I am modifying the code. I wanna set up the list after the file is read finally.
 
The followings is a piece of code,
 
int i = 0;
 
 while ((input = reader.ReadLine()) != null)  // Read about 1,000 times.
                {
                   strarr = input.Split(','); // After this step, the strarr will be like strarr[3] = {1,2,3};
                   intarr.SetValue(Convert.ToInt32(strarr.GetValue(1)), 1); // Convert string[1] to intarr[1].
                   intarr.SetValue(Convert.ToInt32(strarr.GetValue(2)), 2); // Convert string[2] to intarr[2].
                    
                   //strlist.Insert(i,intarr);
                   strlist.Add(intarr);
                   //i++;
                }
 
The problem is, no matter which method I use, e.g.Add or Insert. The numbers in the list are the same. E.g. only last value is added to the list, whereas the previous is lost. So the list will be filled with last value with 1,000 times.
 
I donnot know whether I specify the problem clearly. But it should be added the value current per time, instead of rewriting the previous values...


 
 
Paul Louth





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

You're adding the same intarr to every row.  You must create a new int array for each row.  Try this:

string input;
List<int[]> list = new List<int[]>();

while ((input = reader.ReadLine()) != null)  // Read about 1,000 times.
{
    string parts = input.Split(new char[] { ',' });

    int[] row = new int[3];
    row[0] = Convert.ToInt32(parts[0]);
    row[1] = Convert.ToInt32(parts[1]);
    row[2] = Convert.ToInt32(parts[2]);

    list.Add(row);
}


 
 
Luis in SE





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

Hey Paul,
Thanks for your helps!!! They are really helpful, indeed.
After programming with your hints and codes, my program works right now. :0)
I'd like to mark this post as answered!!!
Have a nice day,
Cordially,
Luis


 
 
Paul Louth





PostPosted: Visual C# General, Newbie in C#, the problem about 2 dimension array descending sort. Top

Excellent, pleased to help :)