Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Sorting a Multi Dimensional Array
 

Sorting a Multi Dimensional Array

Alright so this is what I have so far. I connect to a database with ODBC, I select the proper table and use it to fill a dataset, I send the dataset to a multidimensional array.

This is where I get stuck.

            for (int i = 0; i < count; i++)
            {
                teams[i, 0] = dataset.Tables[0].Rows[i][0].ToString();
                teams[i, 1] = dataset.Tables[0].Rows[i][1].ToString();
                teams[i, 2] = dataset.Tables[0].Rows[i][2].ToString();
                teamsint[i, 0] = Convert.ToInt32(teams[i, 0]);
                teamsint[i, 1] = Convert.ToInt32(teams[i, 1]);
                teamsint[i, 2] = Convert.ToInt32(teams[i, 2]);
                txtLog.AppendText("Column 1: " + teams[i, 0] + "   Column 2: " + teams[i, 1] + "    Column 3: " + teams[i, 2] + "\r\n");
                if (teamsint[i, 1] == 1)
                {
                    Random random = new Random();
                    int seednumber = random.Next(0, 100000);
                    teamsint[i, 3] = seednumber;
                }
            }


The current code works perfectly but I would like to sort them based upon teamsint[i, 3].

After sorting them I want to set the highest 2 teams, the next 2 teams, the next 2 teams, and so on to the same number and store it in teamsint[i, 4]
Basically I am trying to create a random seeding system.
DanBXD  Thursday, July 30, 2009 6:35 AM

using linq , you can do it like,

teamsint.OrderBy(T=> T[3]);


if you can't or don't wanna use linq, refer following
http://www.aspfree.com/c/a/ASP/How-to-Sort-a-Multi-Dimensional-Array/
NareshG  Thursday, July 30, 2009 6:56 AM

using linq , you can do it like,

teamsint.OrderBy(T=> T[3]);


if you can't or don't wanna use linq, refer following
http://www.aspfree.com/c/a/ASP/How-to-Sort-a-Multi-Dimensional-Array/
NareshG  Thursday, July 30, 2009 6:56 AM

Hi DanBXD,

These are other solutions:

1. Put the indexes to another array. Then sort that array based on our own IComparer:
http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=151.

2. Copy the data to a DataTable and then sort that DataTable:
http://stackoverflow.com/questions/232395/how-do-i-sort-a-two-dimensional-array-in-c.

Let me know if this helps.
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, July 31, 2009 8:25 AM

You can use google to search for other answers

Custom Search

More Threads

• new to using datagrids with Windows Forms
• Searching a Form for Checkbox Controls
• DataGridViewCheckBox
• Datagridview - handling external table updates
• inserting excel data into sql server through vb.net
• Drag an Drop a cell from one place to another.
• DataRowView and relationship
• DataGridViewComboBox Gets Disappears !
• display member of checkedlistbox
• ComboBox AutoComplete Weird Behavior