Javascript: Algorithm For Sorting Based On An Predefined Order Of Regexes
Solution 1:
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 = newMap(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 = newMap(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());Solution 2:
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.
constgetOrder = 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)) return1;
        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)) return3;
        return0;
    },
    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; }
Post a Comment for "Javascript: Algorithm For Sorting Based On An Predefined Order Of Regexes"