Home > DeveloperSection > Forums > Sorting two columns in c# using the values in one column
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    March-29-2014 4:19 AM

 C# C# 
Ratings:


 1 Reply(s)
 1039  View(s)
Rate this:

I have two columns

form.myDataTable.Rows[i][2 * cs] = corr;               

form.myDataTable.Rows[i][2 * cs + 1] = "p" + Convert.ToString(col1) + " p" + Convert.ToString(col2);

I need to sort 2*cs column by values and also corresponding names in column 2*cs+1.

I am trying like this: var corrvalues = new Dictionary(); correlationvalues["p" + Convert.ToString(col1) + " p" + Convert.ToString(col2)] = corr; sortedvalues = correlationvalues.Values.OrderByDescending;

I am not clear how to use orderbydescending, i am new to c#. Thanks for help.



Pravesh Singh

Total Post:411

Points:2881
Posted on    March-29-2014 7:05 AM

Hi Samuel,

It is much easier, if you are using databinding.

you can fill the datagrid by

form.myDataTable.DataSource = Enumerable.Range(0, count).Select(i => new { Column1 = corr, Column2 = "p" + Convert.ToString(col1) + " p" + Convert.ToString(col2) }).ToArray();

doing a sort is then also easier

form.myDataTable.DataSource = Enumerable.Range(0, count).Select(i => new { Column1 = corr, Column2 = "p" + Convert.ToString(col1) + " p" + Convert.ToString(col2) }).OrderBy(k => k.Column2).ToArray();


Don't want to miss updates? Please click the below button!

Follow MindStick