Dear All,
I have a column of string data type and having numerical value -1,0,1 and if i try to sorting ascending, it was display as 0,1,-1 but expected is -1,0,1. In this case only all the value are numerical but most of the case it will have combination of numeric,string and special character.
when i compare the same behavioue with excel it was working fine. How to achieve the excpected result.
Regards
Vijay A
Hi Vijay,
If the data is stored as a string, then it will be sorted as a string. The best thing to do would be to use a numeric type instead of strings.
If you cannot do that, then you could use the SortComparer property on the column to sort the data in a custom order. What you have to do is write a class that implements the IComparer interface. There are lots of examples of this here on the forums for all sorts of custom sorting requirements.
Hi Mike,
Thanks for the reply. i cannot convert the data type to numeric type since the column has to support alpha numeric and special characters. I have tried to implement the ICompare interface for sorting. In the compare method i have implemented the following logic.
Xcell.Value.ToString.CompareTo(YCell.Value.ToString());
Expected behavior:
There should not be a difference in sorting the string value in excel and in ultra wingrid.
It would be great if you let me know how to achieve the string sorting in ultra win grid as in excel.
What you have here is exactly what you do NOT want. You are comparing string. What you need to do here is take the value of each cell and examine it and perhaps try to convert it into a numeric value and then compare the numbers.
I'm not sure what you want to do if the string contains alphanumeric characters and cannot be convered into a number, but that's up to you to decide.
Let me clearly give my requirement/need.
i have columns whose datatype is string which has to support alphanumeric and special character. This columns has to support sorting similar to sorting in excel. I have enabled the sorting for these column by using the following command
grd.DisplayLayout.Bands[0].Columns["MyColumn"].SortIndicator = Infragistics.Win.UltraWinGrid.SortIndicator.Ascending;
Then i have entered only the numeric value to columns and sort the columns but i haven't get the expected result.
values are 0,-1,1 and expected result is -1,0,1.
Then i tried implement the icomparer as said above to get the desired result but it went in vain.
It would be helpful, if you provide solution for sorting similar to excel or the behaviour of sorting for the above command if i have values of special character,Upper case,lower case, postive number, negative number and also combination all above.
Thanks for the reply.
If the DataType of your column is a string, the DotNet will sort the data as string data. This is not determined by any Infragistics code. The grid code simply calls the Compare method on the value of the cell, which in this case is a string.
So if you want to know the default behavior, you could test this out by using string variables:
string s1 = "-2";
string s2 = "1";
Debug.Writeline(s1.Compare(s2));
Thanks for the reply. Sorry to bother you again. There is a miscommunication in our discussion. Bascially what i want to know is that if i have column data type as string and having the following values
-2, 1, a, 0, A, !, -1, 3, 2, >. and using the infragistics sort command.
Infragistics.Win.UltraWinGrid.SortIndicator.Ascending.
what will the output look like?
It would be great, if you can also please explain the sorting order in the above case.
Thanks
I'm not sure how the Excel sorting words, exactly, and how it differs from the sorting of strings in DotNet. And I don't have a sample of this to give you. But the basic principle of an IComparer class is that two values are passed into the Compare method and you return a value indicating which one is bigger or if they are the same.
So in a case like this where you have numbers are string, what you need to do is try to convert the string into a numeric value. There's no very simple one-shot way to do this, you are going to have to write a bit of code to handle every situation.
It's very hard for me to guide you here, because I don't know the requirements of your applications and I don't know what assumptions I can make. For example, can a cell contain both letters and numbers? Or is it one of the other?
If a cell can contain both, then I honestly don't know what to do in that case. My guess is that Excel treats such a value as a string and never as a number.
What I would do is get the value of each cell and put that value into a string.
Then you can try to turn that string into a numeric value. I'd probably use the int.TryParse or maybe double.TryParse method.
If the method succeeds, then you can treat that value as a number.
If both cells turn out to be numeric, then you can simply compare the two values.
If both cells fail, then you can simply compare the two strings.
If one is numeric and the other is not, then you have to decide which one comes first and return the appropriate value from the Compare method to indicate that.