skip to Main Content

I’m new to Dart and Flutter. I have a list of strings which contains some paragraphs but I can’t figure out how to sort it based on how much it matches with a string given by the user.

I have tried this but it seems I dons not work

paras.sort((a, b) => b.contains(userPara).compareTo(a.contains(userPara));

2

Answers


  1. That depends on how you define "how much it matches".

    If we say like you want to sort them based on who has the highest amount of equal characters to your match descending, then you would do the following:

      const String match = "123456";
      final List<String> paras = ["1", "123123", "1221222", "12341234", "12345", "121121", "123413"];
    
      paras.sort((String a, String b) {
        int matchCountA = 0;
        int matchCountB = 0;
        for (int i = 0; i < match.length; ++i) {
          if (i < a.length && match[i] == a[i]) {
            matchCountA++;
          }
          if (i < b.length && match[i] == b[i]) {
            matchCountB++;
          }
        }
        if (matchCountA == matchCountB) {
          return 0;
        }
        return matchCountA > matchCountB ? -1 : 1;
      });
    
      print(paras); // [12345, 12341234, 123413, 123123, 1221222, 121121, 1]
    
    

    The compare function should return a positive number if b is ordered before a and a negative number if a is ordered before b. Otherwise it should return 0.

    Login or Signup to reply.
  2. To sort a list of strings based on how much they match with a string given by the user, you can use the sort method along with a custom comparator function. The comparator function should calculate a score for each string based on its similarity to the user’s input and use that score for sorting the list.

    In this example, I’ll show you how to sort the list of strings based on the number of occurrences of the user’s input (userPara) in each string:

       void main() {
        List<String> paras = [
         "This is a sample paragraph containing userPara multiple times. userPara",
         "Another paragraph with userPara at the beginning.",
         "One more paragraph with userPara appearing here and there.",
         "Random text with no userPara matches.",
       ];
    
        String userPara = "userPara"; // Replace with the user's input
    
        paras.sort((a, b) => countOccurrences(b, userPara).compareTo(countOccurrences(a, userPara)));
    
        print(paras);
       }
    
        int countOccurrences(String text, String pattern) {
        int count = 0;
        int index = text.indexOf(pattern);
        while (index != -1) {
        count++;
        index = text.indexOf(pattern, index + pattern.length);
       }
      return count;
     }
    

    In this code, the paras list contains several paragraphs (strings), and we want to sort them based on how many times the userPara string appears in each paragraph. The countOccurrences function calculates the number of occurrences of the userPara string in a given text.

    The sort method is used on the paras list, and the comparator function (a, b) => countOccurrences(b, userPara).compareTo(countOccurrences(a, userPara)) is provided. This function compares the count of occurrences of userPara in the two strings a and b, and it sorts them in descending order (highest count first).

    When you run this code, you should see the list sorted based on the number of occurrences of userPara in each paragraph, with the paragraphs having the most occurrences appearing at the beginning of the list.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search