Question

J'essaie d'écrire un comparateur personnalisé pour trier une liste des résultats de recherche basés sur la similitude. Je voudrais que le terme le plus comme le terme de recherche entré apparaît d'abord dans la liste, suivi de phrases qui commencent par la phrase de recherche, puis toutes les autres valeurs dans l'ordre alpha.

Compte tenu de ce code de test:

  string searchTerm = "fleas";
  List<string> list = new List<string>
                                       {
                                           "cat fleas",
                                           "dog fleas",
                                           "advantage fleas",
                                           "my cat has fleas",
                                           "fleas",
                                           "fleas on my cat"
                                       };

J'essaie d'utiliser ce comparateur:

public class MatchComparer : IComparer<string>
{
    private readonly string _searchTerm;

    public MatchComparer(string searchTerm)
    {
        _searchTerm = searchTerm;
    }

    public int Compare(string x, string y)
    {
        if (x.Equals(_searchTerm) ||
             y.Equals(_searchTerm))
            return 0;

        if (x.StartsWith(_searchTerm) ||
            y.StartsWith(_searchTerm))
            return 0;

        if (x.Contains(_searchTerm))
            return 1;

        if (y.Contains(_searchTerm))
            return 1;

        return x.CompareTo(y);
    }

L'appel list.sort (nouveau matchcomparer (SearchTerm) se traduit dans «Mon chat a des puces» en haut de la liste.

Je pense que je dois faire quelque chose d'étrange / bizarre ici. Quelque chose ne va pas ici ou y a-t-il une meilleure approche de ce que j'essaie de faire?

Merci!

Était-ce utile?

La solution

Vous n'utilisez pas toutes les valeurs de retour possibles pour compareto

-1 == x premier 0 == est égal 1 == y en premier

Tu veux quelque chose de plus comme ça

public class MatchComparer : IComparer<string> 
{ 
    private readonly string _searchTerm; 

    public MatchComparer(string searchTerm) 
    { 
        _searchTerm = searchTerm; 
    } 

    public int Compare(string x, string y) 
    { 
        if (x.Equals(y)) return 0; // Both entries are equal;
        if (x.Equals(_searchTerm)) return -1; // first string is search term so must come first
        if (y.Equals(_searchTerm)) return 1; // second string is search term so must come first
        if (x.StartsWith(_searchTerm)) {
            // first string starts with search term
            // if second string also starts with search term sort alphabetically else first string first
            return (y.StartsWith(_searchTerm)) ? x.CompareTo(y) : -1;
        }; 
        if (y.StartsWith(_searchTerm)) return 1; // second string starts with search term so comes first
        if (x.Contains(_searchTerm)) {
            // first string contains search term
            // if second string also contains the search term sort alphabetically else first string first
            return (y.Contains(_searchTerm)) ? x.CompareTo(y) : -1; 
        }
        if (y.Contains(_searchTerm)) return 1; // second string contains search term so comes first
        return x.CompareTo(y); // fall back on alphabetic
    } 
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top