Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1080
custom sorting
posted

Hello, I have a column RATING in my xamDataGrid, along with other financial columns.
Now I need to sort that column in a LOGICAL way (that could not be the alphabetical way, because I need to sort from [worst ] to [best] and viceversa).
How can I implement this kind of sorting?
Can I add a tag to the field and sort by tag value? Or can I add an hidden field with the correct order and then sort the RATING column using the hidden field?

Thanks, Valerio

Parents
  • 355
    Suggested Answer
    Offline posted

    Valerio,

    There are two pretty straight forward was to solve this. First which is only useable if you wont be populating the different ratings that you can use from a database. If you create an enum like the following:

    public enum Ratings : int
    {
    	Bad = 1,
    	OK = 2,
    	Great = 3,
    	Awesome = 4
    }
    Then expose a property on the object that you are using for each row in the xamWebGrid that is of type Ratings. With this approach it will work all by its self with no additional code needed. 
    That being said if adding a property to an object is not a option then you have to make a custom IComparer to use for the column. The IComparer will take the value that is displayed in the cell and compare all the values in the collection by calling the Compare method I Have provided you with a sample IComparer below:
    public class RatingComparer : IComparer<string>
    {
    	public int Compare(string x, string y)
    	{
    		var weight = getSortPositionForItem(x) - getSortPositionForItem(y);
    		return weight;
    	}
    	//     A signed integer that indicates the relative values of x and y, as shown
    	//     in the following table.Value Condition Less than zero x is less than y. Zero x
    	//     equals y. Greater than zero x is greater than y.
    	private int getSortPositionForItem(string item)
    	{
    		switch (item)
    		{
    			case "Bad":
    				return 1;
    			case "OK":
    				return 2;
    			case "Great":
    				return 3;
    			case "Awesome":
    				return 4;
    			default:
    				return 0;
    		}
    	}
    }
    
    I will put together a sample in a blog post later today. Please let us know if this answered your question.
Reply Children
No Data