My DataView is fun and sorting things alphabetically, and I need this for numerical sorting. I searched this entire Internet and found a lot of ideas on how to sort it using ICompare, but nothing sensible.
So my questions
- How do I implement ICompare for a DataView (look for the code here).
- How to properly decrypt a column full of rows, which are the actual rows, and a column full of numbers (with commas).
I need code to help me with this guys. I was more or less lost in the idea of ICompare and how to implement it in various scenarios, so it would be nice to give a good explanation.
Also, please do not give me links. I am looking for solid answers to this question.
Some kind of code I'm using.
DataView dataView = (DataView)Session["kingdomData"]; dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); gvAllData.DataSource = dataView; gvAllData.DataBind(); private string ConvertSortDirectionToSql(SortDirection sortDirection) { string newSortDirection = String.Empty; if (Session["SortDirection"] == null) { switch (sortDirection) { case SortDirection.Ascending: newSortDirection = "ASC"; break; case SortDirection.Descending: newSortDirection = "DESC"; break; } } else { newSortDirection = Session["SortDirection"].ToString(); switch (newSortDirection) { case "ASC": newSortDirection = "DESC"; break; case "DESC": newSortDirection = "ASC"; break; } } Session["SortDirection"] = newSortDirection; return newSortDirection; }
For this scenario, I dynamically create a data table and put it in the data view, where I put the data view in the grid view, and also remember to put the data view in the session object for sorting capabilities.
When the user calls the grid view to sort the column, I recall the data view in the session object and build the sort expression of the data view as follows:
dataview.sort = e.sortexpression + " " + e.Sortdirection;
Or something like that. What usually happens is suitable for all real strings, such as
Car; Home; Cattle; Zack etc ...
But when I do the same for numeric fields with comma separated values, it turns out something like
900; 800; 700; 600; 200; 120; 1200; 12340; 1,000,000;
See what I'm talking about? It just sorts the elements as alpha sort instead of the natural one. I want my Dataview to NATURALLY sort numerical columns as follows:
120; 200; 600; 700; 800; 900; 1200; 12340; 1,000,000;
Let me know what you can do to help me.
Postscript I have looked through countless articles on how to do this, and they all talk about adding to a list / array and doing it this way, but is there a much more efficient way?