Q: JavaScript: Algorithm for sorting based on an predefined order of Regexes I want to sort an array of clothing sizes based on an predefined order of regular expressions.
The expressions array looks like this:
const sizeOrder = [
  /One Size/,
  /[X/S]+S/i,
  /^S$/i,
  /^M$/i,
  /^L$/i,
  /[X]+L/i,
  /[4-9]XL/i,
  /[0-9,]+/,
];

What would be a neat and efficient way to sort an array, that would for example look like this:
const sizes = [
  '45,5',
  'S',
  'XXS',
  'XXL',
  'XS',
  '4XL',
  'One Size',
  '0',
  '32',
  '42,5',
  '18',
  'XXS/XS',
  'XXXS',
  'L'
];

As a first step I would create buckets for the corresponding regular expressions and if a match exists I push that value to the bucket, like so:
function exampleSort() {
  const bucket = Array.from(new Array(sizeOrder.length), () => []);

  sizes.forEach(size => {
    const i = sizeOrder.findIndex(order => order.test(size));

    if (i > -1) {
      bucket[i].push(size);
    }
  });
}

After that I'd go through each bucket and sort them accordingly and then join those arrays together to one.
But I have two questions:
What about the special case of XXS/XS? How would I sort that bucket so that XXS/XS would be between XXS and XS?
This seems to be an imperative and naive implementation. Is there any other way to do this more efficiently?
This is my expected output:
const sorted = [
  'One Size',
  'XXXS',
  'XXS',
  'XXS/XS',
  'XS',
  'S',
  'L',
  'XXL',
  '4XL',
  '0',
  '18',
  '32',
  '42,5',
  '45,5'
];


A: One option would be to iterate through the strings, pushing to a Map of arrays indexed by the matching regular expression. Then, sort each array, and flatten to get the result.
Note the addition of ^ and $ anchors to ensure that only full matches fulfill a .test.


const sizeOrder = [
  /^One Size$/,
  /^[X/S]+S$/i,
  /^S$/i,
  /^M$/i,
  /^L$/i,
  /^[X]+L$/i,
  /^[4-9]XL$/i,
  /^[0-9,]+$/,
];
const patternMap = new Map(sizeOrder.map(pattern => [pattern, []]));
const sizes = [
  '45,5',
  'S',
  'XXS',
  'XXL',
  'XS',
  '4XL',
  'One Size',
  '0',
  '32',
  '42,5',
  '18',
  'XXS/XS',
  'XXXS',
  'L'
];
sizes.forEach((str) => {
  const matchingPattern = sizeOrder.find(pattern => pattern.test(str));
  patternMap.get(matchingPattern).push(str);
});
const valuesArr = [...patternMap.values()];
valuesArr.forEach(arr => arr.sort((a, b) => b.localeCompare(a)));
// sort the last one from /^[0-9,]+$/ differently:
valuesArr[valuesArr.length - 1].sort();
console.log(valuesArr.flat());


Or, using an array of patterns and an optional associated sorting function, you could do something like:


const sizeOrder = [
  { pattern: /^One Size$/ },
  { pattern: /^[X/S]+S$/i, }, // insert your custom sort logic for XXS/XS here
  { pattern: /^S$/i },
  { pattern: /^M$/i },
  { pattern: /^L$/i },
  { pattern: /^[X]+L$/i },
  { pattern: /^[4-9]XL$/i },
  { pattern: /^[0-9,]+$/, sort: arr => arr.sort() }
];
const patternMap = new Map(sizeOrder.map(({ pattern }) => [pattern, []]));
const sizes = [
  '45,5',
  'S',
  'XXS',
  'XXL',
  'XS',
  '4XL',
  'One Size',
  '0',
  '32',
  '42,5',
  '18',
  'XXS/XS',
  'XXXS',
  'L'
];
sizes.forEach((str) => {
  const { pattern } = sizeOrder.find(({ pattern }) => pattern.test(str));
  patternMap.get(pattern).push(str);
});
const valuesArr = [...patternMap.values()];
valuesArr.forEach((arr, i) => {
  const sort = sizeOrder[i].sort;
  if (sort) {
    sort(arr);
  } else {
    arr.sort((a, b) => b.localeCompare(a));
  }
});
console.log(valuesArr.flat());



A: You could use a staged approach by checking 


*

*if the string conteins a slash, then split this string and take the average value for it,

*if some other patterns are in the string, then return a value, reflecting the order or together with an offset a value for 'S', 'M' 'L',

*if the string contains some leading 'X' or a digit followed by 'S' or 'L', then get the number or the count of starting 'X' and use a factor for moving the value to the right direction.


const
    getOrder = s => {
        var standard = { s: -0.5, m: 0, l: 0.5 },
            x = 0;
        if (s.includes('/')) return s.split('/').map(getOrder).reduce((a, b) => a + b) / 2;
        if (/^One Size$/.test(s)) return 1;
        if (/^[lms]$/i.test(s)) return standard[s.toLowerCase()] + 2;
        if (/x+[sl]$/i.test(s)) {
            if (/^\dx/i.test(s)) {
                x = s[0];
            } else {
                while (s[x].toLowerCase() === 'x') x++;
            }
            return standard[s.slice(-1).toLowerCase()] * (1 + 0.01 * x) + 2;
        }
        if (/^[0-9,]+$/.test(s)) return 3;
        return 0;
    },
    sizes = ['45,5', 'S', 'XXS', 'XXL', 'XS', '4XL', 'One Size', '0', '32', '42,5', '18', 'XXS/XS', 'XXXS', 'L', 'M'];

sizes.sort((a, b) => getOrder(a) - getOrder(b) || a.replace(',', '.') - b.replace(',', '.'));

console.log(sizes);
.as-console-wrapper { max-height: 100% !important; top: 0; }


