I have an array which is
[
'B. Wound Bed',
'A. Wound Location and Measurements',
'D. Drainage',
'B. Wound Bed',
'C. Surrounding Tissue',
'Haroon Question Group Without Show PCP',
'D. Drainage',
'Haroon Question Group With Show PCP',
'A. Wound Location and Measurements',
'C. Surrounding Tissue',
];
i want to sort it in such a way that it should return like this
[
'A. Wound Location and Measurements',
'B. Wound Bed',
'C. Surrounding Tissue',
'D. Drainage',
'A. Wound Location and Measurements',
'B. Wound Bed',
'C. Surrounding Tissue',
'D. Drainage',
'Haroon Question Group With Show PCP',
'Haroon Question Group Without Show PCP'
]
It should complete the whole A-Z set and then start again from A-Z
I have tried this one but it doesn’t give the desired result
'B. Wound Bed',
'A. Wound Location and Measurements',
'D. Drainage',
'B. Wound Bed',
'C. Surrounding Tissue',
'Haroon Question Group Without Show PCP',
'D. Drainage',
'Haroon Question Group With Show PCP',
'A. Wound Location and Measurements',
'C. Surrounding Tissue',
];
arr.sort((a, b) => {
let aIndex = parseInt(a.charAt(0), 36);
let bIndex = parseInt(b.charAt(0), 36);
if (aIndex < bIndex) return -1;
if (aIndex > bIndex) return 1;
return 0;
});
console.log(arr);
it returns something like this
"A. Wound Location and Measurements",
"A. Wound Location and Measurements",
"B. Wound Bed",
"B. Wound Bed",
"C. Surrounding Tissue",
"C. Surrounding Tissue",
"D. Drainage",
"D. Drainage",
"Haroon Question Group Without Show PCP",
"Haroon Question Group With Show PCP"
2
Answers
Could you please provide some more information? I created an example, please test it if it’s as you want it to be.
You could distribute the entries over buckets, where there is a bucket for each letter (A-Z) and a catch-all bucket for all other entries.
Then consume these buckets in a cyclic way to populate the result array: