My DataView is acting funny and it is sorting things alphabetically and I need it to sort things numerically. I have looked all across the web for this one and found many ideas on how to sort it with ICompare, but nothing really solid.
So my questions are
- How do I implement ICompare on a DataView (Looking for code here).
- How to correctly decipher from a column full of strings that are actual strings and a column full of numbers(with commas).
I need code to help me out with this one guys. I am more or less lost on the idea of ICompare and how to implement in different scenarios so an over all good explanation would be great.
Also, Please don't hand me links. I am looking for solid answers on this one.
Some Code that I use.
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 the scenario, I build a datatable dynamically and shove it into a dataview where I put the dataview into a gridview while also remembering to put the dataview into a session object for sorting capabilities.
When the user calls on the gridview to sort a column, I recall the dataview in the session object and build the dataview sorting expression like this:
dataview.sort = e.sortexpression + " " + e.Sortdirection;
Or something along those lines. So what ussually comes out is right for all real strings such as
Car; Home; scott; zach etc...
But when I do the same for number fields WITH comma seperated values it comes out something like
900; 800; 700; 600; 200; 120; 1,200; 12,340; 1,000,000;
See what I mean? It just sorts the items as an alpha sort instead of a Natural sort. I want to make my Dataview NATURALLY sort the numeric columns correctly like
120; 200; 600; 700; 800; 900; 1,200; 12,340; 1,000,000;
Let me know what you can do to help me out.
P.S. I have looked through countless articles on how to do this and all of them say to shove into a List/Array and do it that way, but is there a much more efficient way?