skip to Main Content

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


  1. Could you please provide some more information? I created an example, please test it if it’s as you want it to be.

    function sortArray(arr) {
      // Function to check if an item belongs to the A-D set
      const isADSet = item => /^(A|B|C|D)./.test(item);
    
      // Split the array into AD set items and others
      const adSetItems = arr.filter(isADSet);
      const otherItems = arr.filter(item => !isADSet(item));
    
      // Since AD set items should maintain their order and appear twice,
      // and considering they are already in order within each set,
      // we concatenate them directly. For otherItems, we sort them as per the requirement.
      otherItems.sort((a, b) => {
        if (a.includes('With Show PCP') && b.includes('Without Show PCP')) return -1;
        if (a.includes('Without Show PCP') && b.includes('With Show PCP')) return 1;
        return 0;
      });
    
      // Concatenate the sorted parts in the required order
      return [...adSetItems, ...adSetItems, ...otherItems];
    }
    
    // Original array
    const items = [
      'A. Wound Location and Measurements',
      'B. Wound Bed',
      'C. Surrounding Tissue',
      'D. Drainage',
      'Haroon Question Group Without Show PCP',
      'Haroon Question Group With Show PCP',
      'A. Wound Location and Measurements',
      'B. Wound Bed',
      'C. Surrounding Tissue',
      'D. Drainage',
    ];
    
    // Sorted array
    console.log(sortArray(items));
    Login or Signup to reply.
  2. 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:

    const data = [
      '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',
    ];
    
    
    // Split the entries in their buckets based on the starting letter:
    let buckets = Array.from({length: 27}, () => []);
    for (const s of data) {
        buckets[/^[A-Z]./.test(s) ? s.charCodeAt() - 65 : 26].push(s);
    }
    // The last bucket is a catch-all for elements that do not match the pattern
    const last = buckets.pop();
    
    // Consume the buckets one by one in cyclic order until we have all of them
    const sorted = [];
    while (buckets.length) {
        buckets = buckets.filter(arr => arr.length);
        for (const bucket of buckets) {
            sorted.push(bucket.shift());
        }
    }
    sorted.push(...last); // Add the unsorted entries
    console.log(sorted);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search