Can anyone tell me why this isn’t sorting correctly?
let input =
"Every book is a quotation; and every house is a quotation out of all forests, and mines, and stone quarries; and every man is a quotation from all his ancestors. ";
function wordCountEngine(document) {
let regEx = /[^\w\s]/g;
let clean_input = document.trim().toLowerCase().replace(regEx,'').split(' ');
let word_list = [];
return clean_input.reduce((ans, current, idx, source)=> {
let count = source.filter((val) => val === current).length;
let isCounted = word_list.includes(current);
word_list.push(current);
return (isCounted) ? ans : [...ans,[current, count.toString()]];
},[]).sort((a,b)=> parseInt(a[1]) < parseInt(b[1]));
}
console.log(wordCountEngine(input));
Expected output
[["and","4"],["every","3"],["is","3"],["a","3"],["quotation","3"],["all","2"],["book","1"],["house","1"],["out","1"],["of","1"],["forests","1"],["mines","1"],["stone","1"],["quarries","1"],["man","1"],["from","1"],["his","1"],["ancestors","1"]]
Actual output
[ [ 'all', '2' ], [ 'every', '3' ], [ 'is', '3' ], [ 'a', '3' ], [ 'quotation', '3' ], [ 'and', '4' ], [ 'stone', '1' ], [ 'house', '1' ], [ 'of', '1' ], [ 'book', '1' ], [ 'forests', '1' ], [ 'mines', '1' ], [ 'out', '1' ], [ 'quarries', '1' ], [ 'man', '1' ], [ 'from', '1' ], [ 'his', '1' ], [ 'ancestors', '1' ] ]
I’m not at all sure how the ‘and’ and ‘all’ arrays are mixed up.