Click here to Skip to main content
15,069,665 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
Richard MacCutchan 5-Mar-14 7:01am
   
Why are you tryng to convert real numbers to decimal? Use proper signed float values and your sort should work.
FarhanShariff 5-Mar-14 7:37am
   
How to change it . when i try to change it says Expected type is Decimal.
Richard MacCutchan 5-Mar-14 7:45am
   
Then store a Decimal, not a String. You need to be consistent with your use of types.
Richard MacCutchan 5-Mar-14 7:49am
   
Please do not keep posting code blocks in the comments section. Edit your original question, add the latest code (within <pre> tags as I have done), and provide exact details of what is happening.
FarhanShariff 5-Mar-14 7:51am
   
ok wait let me edit it
FarhanShariff 5-Mar-14 8:31am
   
please check this post you will understand better http://www.codeproject.com/Questions/739262/Compare-tables-and-sort-the-rd-table
FarhanShariff 5-Mar-14 8:01am
   
I have changed decimal to string and removed contertodecimal... now -ve sign is not considered and instead of smallest to largest it is other way round
Member 4330673 13-Jun-15 11:04am
   
I have a similar issue with the datatable

System.Data.DataView view = new System.Data.DataView(yourOriginalTable);
System.Data.DataTable selected =
view.ToTable("Selected", false, "SKU", "AttributeName", "Value");

DataTable dtCloned = selected.Clone();
dtCloned.Columns[2].DataType = Type.GetType("System.Decimal");
foreach (DataRow row in selected.Rows)
{
dtCloned.ImportRow(row);
}
dtCloned.DefaultView.Sort = "Value ASC";

But Value column is not sorting, 10.0 value should go to the bottom while its showing
on top that's the issue. help me to sort it out

Value Column having values:
10.0
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5

1 solution

   
Comments
   
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