I am trying to create a function that will search for all matches in a given string and return the results ordered by the number of matches. For example, if I have the following strings:
var strArray = [
"This is my number one string",
"Another string that contains number",
"Just for example string"
];
// The expected result array after searching for "another number" should be: [1, 0]
Currently, my code searches for matches in a string and returns all indexes where there is at least one match. However, I want the result array to be sorted by the maximum count of matches.
function findMatch(list, phrase) {
var preparedList = [],
value = "";
if (config.get("list").match.enabled) {
for (var i = 0, length = list.length; i < length; i += 1) {
value = config.get("getValue")(list[i]);
var words = phrase.split(' ');
var listMatchArr = [];
$.each(words, function(idx, word) {
var W = word.replace(/[\W_]+/g, ""); // matching only alphanumeric characters
if (match(value, W) && $.inArray(i, listMatchArr) == -1) {
preparedList.push(list[i]);
listMatchArr.push(i);
};
});
}
} else {
preparedList = list;
}
return preparedList;
}