Skip to main content Skip to footer

How to perform a custom sort on clothing sizes in FlexGrid

FlexGrid provides a Sort method which can be directly used to sort the contents of the grid based on a column. If you need more flexibility, you need to create a class that implements the IComparer interface.

Use Case: Sorting Sizes of Clothing Garments

Consider the example where we want to sort items by particular garment size (XS, S, M, L, XL and XXL) in a store. These values are not numeric, and sorting alphabetically is not ideal because S (Small) would appear between M (Medium) and XS (Extra Small). A separate comparer should be used for this purpose.

IComparer Interface

The System.Collections.IComparer interface has a single method called System.Collections.IComparer.Compare(System.Object,System.Object) that takes two objects as arguments (in this case, they will be Row objects) and returns -1, 0, or +1. This interface is used in conjunction with the Sort method and provides a way to customize the sort order of a collection. The advantage of using IComparer is that you can create a number of sorting options, and you have the option of passing more specific and more appropriate sorting criteria to the Sort method. IComparer allows you to define re-usable, well-encapsulated comparers. This is especially useful if the comparison needs to know some additional information.

Applying Custom Sort on C1FlexGrid

Among the five overloads of the Sort method in C1FlexGrid for WinForms, two accept an object of IComparer interface as one of their arguments and hence, can be used to sort a group of rows using the specified comparer. In order to apply sorting based on the sizes (XS, S, M, L, XL and XXL) available for the garments, implement IComparer interface in a class. This class will be a ‘comparer’ and will impose the sorting order. To sort the grid using IComparer, create a new instance of the class that implements it and pass that instance to the Sort method of the grid.

private void sort_Click(object sender, EventArgs e)
{
    c1FlexGrid1.Sort(new MyComparer(c1FlexGrid1)); // Perform a custom sort on the grid.  
}

public class MyComparer : IComparer
{
    C1FlexGrid cfg;
    public MyComparer(C1FlexGrid cfg)
    {
        this.cfg = cfg;
    }
    public int Compare(object r1, object r2)
    {
        int cmp = 0;
        string s1 = cfg[((Row)r1).Index, 3].ToString();
        string s2 = cfg[((Row)r2).Index, 3].ToString();
        if (s1 == "XS")
        {
            if (s2 == "XS")
                cmp = 0;
            else
                cmp = -1;
        }
        else if (s1 == "XXL")
        {
            if (s2 == "XXL")
                cmp = 0;
            else
                cmp = 1;
        }
        else if (s1 == "S")
        {
            if (s2 == "XS")
                cmp = 1;
            else if (s2 == "S")
                cmp = 0;
            else
                cmp = -1;
        }
        else if (s1 == "L")
        {
            if (s2 == "XS")
                cmp = 1;
            else if (s2 == "S")
                cmp = 1;
            else if (s2 == "M")
                cmp = 1;
            else if (s2 == "L")
                cmp = 0;
            else
                cmp = -1;
        }
        else if (s1 == "M")
        {
            if (s2 == "XS")
                cmp = 1;
            else if (s2 == "S")
                cmp = 1;
            else if (s2 == "M")
                cmp = 0;
            else
                cmp = -1;
        }
        else if (s1 == "XL")
        {
            if (s2 == "XXL")
                cmp = -1;
            else if (s2 == "XL")
                cmp = 0;
            else
                cmp = 1;
        }
        return cmp;
    }
}