I want to sort list of words depending on its position in a given string.
/// "Karim is an engineer"
/// ["engine","karim is","an"]
/// After sorting it will be:
/// ["Karim is","an",engine"]
CodePudding user response:
It is not really sorting... what i do ? i create an empty array. if a word in the given string is found in my list of words to be sorted, then i add that word to the empty array. However, the result is what expected.
List<String> yourList = ["engineer", "Karim", "an", "is"];
List<String> result = [];
String givenString = "Karim is an engineer";
List<String> wordsInStr = givenString.split(' ');
for (String wrd in wordsInStr) {
if (yourList.contains(wrd)) {
result.add(wrd);
}
}
print(result);
}