sortCustom

 avatar
user_8454105
plain_text
a year ago
1.1 kB
6
Indexable
import { computed } from 'vue';

export default {
  // Assuming `data` is the function that returns your initial data
  data() {
    return {
      items: [
        // Your array of objects here
      ]
    }
  },
  computed: {
    sortedItems() {
      // Assign each prefix a priority
      const order = ['company', 'pet', 'plans', 'contact', 'summary', 'ny'];
      return this.items.sort((a, b) => {
        const keyA = Object.keys(a).find(key => order.some(prefix => key.startsWith(prefix)));
        const keyB = Object.keys(b).find(key => order.some(prefix => key.startsWith(prefix)));
        
        const priorityA = keyA ? order.findIndex(prefix => keyA.startsWith(prefix)) : order.length;
        const priorityB = keyB ? order.findIndex(prefix => keyB.startsWith(prefix)) : order.length;
        
        // Compare by priority, then by key alphabetically if the same priority
        if (priorityA !== priorityB) {
          return priorityA - priorityB;
        } else {
          return keyA.localeCompare(keyB);
        }
      });
    }
  }
}
Editor is loading...
Leave a Comment