Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Datatable dt

The values in the DataTable are both +ve and -ve.
I need to sort the values from smallest to Largest


When I sort in ASC order the least value is at the bottom
I have to traverse all columns to sort the DataTable
How can I sort the Table
_____________________________________
New1 | New2 | New3 |
____________________________________|
-0.1646 | 1.21 | 1.311
-0.1431 | 1.25 | -1.313
-0.1445 | 1.31 | 1.314
-0.1259 | 1.32 | -1.312
-0.1274 | 1.37 | 1.313
-0.1507 | 1.39 | -1.315

C#
private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
    DataTable table3 = new DataTable();
    DataRow dr = null;
    string filterExp = string.Empty;
    for (int i = 0; i < table1.Rows.Count; i++)
    {

        string col = table1.Rows[i]["Par Name"].ToString();
        if (table2.Columns.Contains(col))
        {
            if (!table3.Columns.Contains(col))
            {
                table3.Columns.Add(col, typeof(string));
                filterExp = filterExp + col + " asc ,";
            }

            for (int j = 0; j < table2.Rows.Count; j++)
            {
                if (table3.Rows.Count != table2.Rows.Count)
                {
                    dr = table3.NewRow();
                    table3.Rows.Add(dr);
                }
                table3.Rows[j][col] = (table2.Rows[j][col]).ToString()
            }


        }

    }
    DataView dv = new DataView(table3);
    filterExp = filterExp.TrimEnd(',');
    dv.Sort = filterExp;
    table3 = dv.ToTable();

    return table3;
}
Posted
Updated 5-Mar-14 1:59am
v6
Comments
Richard MacCutchan 4-Mar-14 11:49am    
That looks like it has been sorted on NEW2, not NEW1.
FarhanShariff 5-Mar-14 6:03am    
The table3 was built by comparing two other table1 and table2.
I want to sort table3 from smallest to largest
Negative values it is not considering negative sign when looking for smallest Value.In case of Negative Values larger the value smaller is the number
FarhanShariff 5-Mar-14 6:06am    
private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
DataTable table3 = new DataTable();
DataRow dr = null;
string filterExp = string.Empty;
for (int i = 0; i < table1.Rows.Count; i++)
{

string col = table1.Rows[i]["Par Name"].ToString();
if (table2.Columns.Contains(col))
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof(decimal));
filterExp = filterExp + col + " asc ,";
}

for (int j = 0; j < table2.Rows.Count; j++)
{
if (table3.Rows.Count != table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
table3.Rows[j][col] = Convert.ToDecimal((table2.Rows[j][col]).ToString());
}


}


}
DataView dv = new DataView(table3);
filterExp = filterExp.TrimEnd(',');
dv.Sort = filterExp;
table3 = dv.ToTable();

return table3;
}
Amalraj Ramesh 4-Mar-14 11:52am    
you already done it..

DataView dv = table2.DefaultView;
dv.Sort = "NEW1 ASC";
DataTable sortedDT = dv.ToTable();

What is the problem here..?
FarhanShariff 5-Mar-14 4:17am    
The table has negative values it is not considering negative sign when looking for smallest Value

1 solution

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 17:26pm    
5ed.
—SA
Maciej Los 4-Mar-14 17:27pm    
Thank you, Sergey ;)
FarhanShariff 5-Mar-14 3:47am    
The table has negative values it is not considering negative sign when looking for smallest Value.In case of Negative Values larger the value smaller is the number

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900