tags:

views:

188

answers:

1

In the control ObjectListView(http://objectlistview.sourceforge.net/html/cookbook.htm), I'm trying to add a custom sorter where it ignores "The" and "A" prefixes.

I managed to do it with a regular ListView, but now that I switched to ObjectListView(a lot more features, and ease), I can't seem to do the same.

The following is the Main comparer in the ObjectListView code i think...

public int Compare(object x, object y)
    {
        return this.Compare((OLVListItem)x, (OLVListItem)y);
    }

Original Sorter for ascending, as an example (Ignoring "A" and "The")

public class CustomSortAsc : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                string[] px = Convert.ToString(x).Split(' ');
                string[] py = Convert.ToString(y).Split(' ');
                string newX = "";
                string newY = "";

                for (int i = 0; i < px.Length; i++)
                {
                    px[i] = px[i].Replace("{", "");
                    px[i] = px[i].Replace("}", "");
                }
                for (int i = 0; i < py.Length; i++)
                {
                    py[i] = py[i].Replace("{", "");
                    py[i] = py[i].Replace("}", "");
                }

                if ((px[1].ToLower() == "a") || (px[1].ToLower() == "the"))
                {
                    if (px.Length > 1)
                    {
                        for (int i = 2; i < px.Length; i++)
                            newX += px[i];
                    }
                }
                else
                {
                    for (int i = 1; i < px.Length; i++)
                        newX += px[i];
                }

                if ((py[1].ToLower() == "a") || (py[1].ToLower() == "the"))
                {
                    if (py.Length > 1)
                    {
                        for (int i = 2; i < py.Length; i++)
                            newY += py[i];
                    }
                }
                else
                {
                    for (int i = 1; i < py.Length; i++)
                        newY += py[i];
                }


                return ((new CaseInsensitiveComparer()).Compare(newX, newY));
            }
A: 

Install a CustomSorter delegate, and in that delegate, put an ListItemSorter onto the ObjectListView

this.incidentListView.CustomSorter = delegate(OLVColumn column, SortOrder order) {
     this.incidentListView.ListViewItemSorter = new CustomSortAsc();
};

See this recipe on sorting

Better than doing all this work on each comparison, cache the sort value on each of your model objects. If the value is "{The} Whole Nine Yards", store "whole nine yards" and do a simple (and fast) string compare on those values.

ObjectListView does have its own forum.

Grammarian