ObjectListView: Custom Sorter

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2009-10-23T20:25:50Z Indexed on 2010/06/18 16:03 UTC
Read the original article Hit count: 432

Filed under:
|

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));
            }

© Stack Overflow or respective owner

Related posts about c#

Related posts about listview