JSFiddle - React, Tailwind, and code Playground
by amindunited
JavaScript
let first = '/a/new/path/$__var__';
let second = '/a/new/path/123';
let third = '/a/$__id__/path/$__var__';
let fourth = '/a/new/path/with/a/very/specific/route';
let fifth = '/$__start__/new/path/144';
console.log(first.match(/\$__(\w*)__/).index);
// const arr = [first, second, third];
const arr = [fifth, third, second, first, fourth, fifth];
const sorted = arr.sort((a, b) => {
const aMatch = a.match(/\$__(\w*)__/);
const bMatch = b.match(/\$__(\w*)__/);
if (!aMatch && !bMatch) {
return b.match(/\//g).length - a.match(/\//g).length;
} else if (!aMatch) {
return -1;
} else if (!bMatch) {
return 1;
} else {
console.log('else ', a, b, ':', aMatch.index, bMatch.index);
return bMatch.index - aMatch.index;
}
})
console.log('sorted ', sorted);