const currArray = [
".tag-hash-order-2.tag-hash-written-section-001-002",
".tag-hash-written-section-001-001.tag-hash-order-1",
".tag-hash-written-section-001-001.tag-hash-order-2",
".tag-hash-order-6.tag-hash-written-section-001-001",
".tag-hash-order-5.tag-hash-written-section-001-001",
".tag-hash-order-6.tag-hash-written-section-001-002",
".tag-hash-order-6.tag-hash-written-section-001-003",
];
const newArray = currArray
.reduce((allElements, element) => {
const num = parseInt(
element
.split(".")
.filter((el) => el.includes("tag-hash-written-section"))[0]
.replace("tag-hash-written-section-", "")
.split("-")[1]
);
allElements[num] = allElements[num]
? allElements[num].concat([element]).sort((firstEl, secondEl) => {
const firsNumber = parseInt(
firstEl
.split(".")
.filter((el) => el.includes("tag-hash-order"))[0]
.replace("tag-hash-order-", "")
);
const secondNumber = parseInt(
secondEl
.split(".")
.filter((el) => el.includes("tag-hash-order"))[0]
.replace("tag-hash-order-", "")
);
return firsNumber < secondNumber
? -1
: firsNumber > secondNumber
? 1
: 0;
})
: [element];
return allElements;
}, [])
.flat();