SORTING TWO COLUMNS IN C# USING THE VALUES IN ONE COLUMN

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
C# 
C#
 1267  View(s)
Ratings:
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.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Sorting two columns in c# using the values in one column

    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();

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!