Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore "The" and "A" when sorting a listview in C#

Currently I am making a mini music player / organizer for myself. However, when using a list view, it sorts alphabetically, and doesn't ignore "The" and "A":

  1. A Lot Like Me
  2. Adiago For Strings
  3. Stay Crunchy
  4. The Foresaken
  5. Time to Pretend

should be:

  1. Adiago For Strings
  2. The Foresaken
  3. A Lot Like Me
  4. Stay Crunchy
  5. Time To Pretend

It's all loaded from a multi-dimensional array, and I've even tried filtering out "The" and "A" manually, and then display the real name (from a different array), but then it just sorts the displayed name (including "The" and "A")

like image 648
Mike Avatar asked Dec 29 '22 14:12

Mike


2 Answers

What you could do is to create a customer comparer and set it on your ListView instance using the ListView.ListViewItemSorter property. Then, your comparer is responsible for remvoing "the" and "a" from the start of the items being compared.

When your ListView is sorted, it will use this custom comparer to sort with, but the original values including "the" and "a" will be used as display values in the ListView (ie. you do not need to modify the values you put in the ListView - the comparer just ignores the words you want it to when sorting).

like image 109
adrianbanks Avatar answered Jan 12 '23 18:01

adrianbanks


You could do it with a custom comparison method like this:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Example
{
    static void Main()
    {
        List<String> names = new List<String>
        {
            "A Lot Like Me",
            "Adiago For Strings",
            "Stay Crunchy",
            "The Foresaken",
            "Time to Pretend"
        };

        names.Sort(smartCompare);
    }

    static Regex smartCompareExpression
        = new Regex(@"^(?:A|The)\s*",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.IgnoreCase);

    static Int32 smartCompare(String x, String y)
    {
        x = smartCompareExpression.Replace(x, "");
        y = smartCompareExpression.Replace(y, "");

        return x.CompareTo(y);
    }
}

The regular expression strips off any leading "A " or "The " from the strings so that they won't effect the comparison.

like image 44
Andrew Hare Avatar answered Jan 12 '23 18:01

Andrew Hare