I have an string array
const arr = ['ACST', 'CST', 'CCST'];;
And i have const word = 'CS';
How to sort array to be
const newArr = ['CST', 'CCST', 'ACST'];
I tried something like this, but it looks bad, and doesn't work correct;
Output CCST -> CST ... , should be CST -> CCST
arr.sort((a, b) => {
let A = 0;
let B = 0;
for (let i = 0; i < word.length; i++) {
if (a.charAt(i) === word.charAt(i)) {
A++;
}
if (b.charAt(i) === word.charAt(i)) {
B++;
}
}
return A + B;
});