Hi,
I'm currently trying to sort out some data in a XamDataGrid in a specific order. Lets say I have 3 columns which has data like this:
ID | Category | Capacity
1 | Games | 100
2 | Cars | 150
3 | Sports | 65
4 | Planes | 100
5 | Games | 50
6 | Cars | 10
The problem is that I want to sort the categories column in a certain order - Cars, Planes, Sports and Games - Like below:
How can I go about doing this? I was thinking of having the list in a dictionary or something and using the Sort Comparer method in XamDataGrid - however, I'm not sure as to how to implement this. Any help would be appreciated. Thanks in advance.
Ananya
Hello Ananya,
I have created a simple sample application based on your description in order to show you how can implement custom Comparer for Category field as using Dictionary. Basically I have created MyComparer class that implements IComparer and in the Comparer method I have added Dictionary<int, string> sortingPriority:
Dictionary<int, string> sortingPriority = new Dictionary<int, string>(); sortingPriority.Add(0, "Cars");sortingPriority.Add(1, "Planes");sortingPriority.Add(2, "Sports");sortingPriority.Add(3, "Games"); var categoryItemsX = sortingPriority.Where(xx => xx.Value.ToString() == x.ToString()).ToList(); var categoryItemsY = sortingPriority.Where(yy => yy.Value.ToString() == y.ToString()).ToList();
sortingPriority.Add(0, "Cars");sortingPriority.Add(1, "Planes");sortingPriority.Add(2, "Sports");sortingPriority.Add(3, "Games");
var categoryItemsX = sortingPriority.Where(xx => xx.Value.ToString() == x.ToString()).ToList(); var categoryItemsY = sortingPriority.Where(yy => yy.Value.ToString() == y.ToString()).ToList();
Then I have used the Tkey value to determine the priority of current items, for example:
if (categoryItemsX[0].Key.Equals(categoryItemsY[0].Key)) return 0; if (categoryItemsX[0].Key > categoryItemsY[0].Key) return 1; if (categoryItemsX[0].Key < categoryItemsY[0].Key) return -1; return -1;
if (categoryItemsX[0].Key > categoryItemsY[0].Key) return 1;
if (categoryItemsX[0].Key < categoryItemsY[0].Key) return -1;
return -1;
Please let me know if you require any further assistance regarding this mater.
Sincerely, ZhivkoEntry Level Software Developer