Domanda

Sto cercando di scrivere un comparatore personalizzato per ordinare un elenco di risultati di ricerca in base alla somiglianza.Vorrei che il termine più come il termine di ricerca inserito per apparire prima nell'elenco, seguito da frasi che iniziano con la frase di ricerca, quindi tutti gli altri valori in ordine alfa.

Dato questo codice di prova:

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

Sto cercando di usare questo comparatore:

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

Calling List.Sort (New MatchComparer (SearchTerm) si traduce in "il mio gatto ha flessi" nella parte superiore della lista.

Penso di dover fare qualcosa di strano / strano qui .. è qualcosa di sbagliato qui o c'è un approccio migliore a quello che sto cercando di fare?

Grazie!

È stato utile?

Soluzione

Non stai usando tutti i possibili valori di ritorno per confronto

-1== x prima 0== sono uguali 1== y prima

vuoi qualcosa di più simile a questo

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
    } 
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top