Given this array = [ "3ab1", "2a0", "1abc2" ]
How do I sort it to [ "1abc2", "3ab1", "2a0" ]
(descending order of the last number)
and return [ 1,3,2 ]
. (the first numbers of each term)
When the last number and the next last number is not consecutive, the value returned should be 0.
[ "2x2", "3x0", "2x1" ] ==> [ 2, 2, 3 ]
[ "22x0", "3x9", "2x1" ] ==> [ 3, 0, 0, 0, 0, 0, 0, 0, 2, 22 ]
[ "2x4", "3x0" ] ==> [ 2, 0, 0, 3 ]
[ "axn", "bx(n-2)" ] ==> [ "axn", "0x(n-1)", bx(n-2) ] ==> [ a, 0, b ]
I was thinking of converting to the array to string, replacing the number and letters in front and then sorting the array. But I do not know how put the part that was replaced back to its original number. This is my attempt on returning the final array once it is sorted.
var ary = [ "1abc2", "3ab1", "2a0" ];
console.log(((ary.toString()).match(/\d+(?!,)/g)).slice(0, -1));
I saw these questions on sorting arrays based on numbers but they do not seem to work for me.