---
title: "Sorting two columns in c# using the values in one column"  
description: "Sorting two columns in c# using the values in one column"  
author: "Samuel Fernandes"  
published: 2014-03-29  
updated: 2014-03-29  
canonical: https://www.mindstick.com/forum/2000/sorting-two-columns-in-c-sharp-using-the-values-in-one-column  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Sorting two columns in c# using the values in one column

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](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) by [values](https://www.mindstick.com/forum/327/sum-textbox-values) and also corresponding names in column 2*cs+1.

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) like this: [var](https://www.mindstick.com/forum/33920/what-is-different-var-and-dynamic-types-in-c-sharp) corrvalues = new [Dictionary](https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp)(); 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.

## Replies

### Reply by Pravesh Singh

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();


---

Original Source: https://www.mindstick.com/forum/2000/sorting-two-columns-in-c-sharp-using-the-values-in-one-column

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
