Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i use this code to sort a table`s rows by values from a column but it doesnt do what i want for example it should sort the rows like this:
0; 0.1;1;2;....9;10 but my code sort`s like this : 0;0.1;1;10;2;...9; So its there any other way to do this sorting right(the sort of rows must be ascending by values from a column) or can someone tell me where i messed up?

C#
public static DataTable sort(DataTable dt)
      {


         dt.DefaultView.Sort="["+dt.Columns["Distanta Euclidiana"]+"] asc";

          return dt;
      }
Posted
Comments
Herman<T>.Instance 2-Apr-12 7:13am    
What is the dataype of the column you sort on? It looks like Varchar but you want to sort it as decimal.

1 solution

I think the Data type of the column being sorted is string, in which case, if sorted in ascending order, 10 comes before 2 as the sorting is done from the left to right and character '1' comes before the character '2'.
If the data type of column is set to any of numeric types say double, then the desired sorting as specified can be achieved.
 
Share this answer
 
Comments
aryx123 2-Apr-12 8:19am    
Thank you,the column maping(i think this is the name) , dt.Columns.Add("Distanta Euclidiana",Type.GetType("System.Double"));, just did the trick
aryx123 2-Apr-12 8:24am    
the data type was double but my table memorizet it as string i think
ProEnggSoft 2-Apr-12 8:29am    
You're welcome.

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