如何实现JavaScript数组自定义排序,将'Primary'置于索引0、'Secondary'置于索引1且不区分大小写?
Solution for Prioritizing 'Primary' and 'Secondary' in Array Sorting
First, let's break down your core requirements clearly:
- Any case-insensitive match for 'Primary' should sit at index 0 of the array
- Any case-insensitive match for 'Secondary' should sit at index 1 (right after 'Primary')
- The remaining elements need to be sorted alphabetically (case-insensitive)
- The solution should handle dynamic arrays where these elements might appear in any position or case
Approach 1: Exact Positioning (Single Instance at Index 0/1)
This method moves one instance of 'Primary' to the first position, one instance of 'Secondary' to the second, then sorts all remaining elements (including duplicates of these two values) alphabetically.
function prioritizeCardOrder(cards) { // Create a copy to avoid modifying the original array directly const cardCopy = [...cards]; const prioritized = []; // Find and move the first case-insensitive 'Primary' to our result const primaryIdx = cardCopy.findIndex(card => card.toLowerCase() === 'primary'); if (primaryIdx !== -1) { prioritized.push(cardCopy.splice(primaryIdx, 1)[0]); } // Find and move the first case-insensitive 'Secondary' to our result const secondaryIdx = cardCopy.findIndex(card => card.toLowerCase() === 'secondary'); if (secondaryIdx !== -1) { prioritized.push(cardCopy.splice(secondaryIdx, 1)[0]); } // Sort remaining elements case-insensitively for consistent ordering cardCopy.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); // Combine our prioritized elements with the sorted rest return [...prioritized, ...cardCopy]; } // Example usage: let Card = ['BBC', 'primary', 'DDC', 'secondary', 'Abccc', 'Primary']; Card = prioritizeCardOrder(Card); console.log(Card); // Output: ['primary', 'secondary', 'Abccc', 'BBC', 'DDC', 'Primary']
Approach 2: Group All Matching Instances
If you want all case-insensitive matches for 'Primary' to come first (as a group), followed by all 'Secondary' matches, then the rest sorted alphabetically, use this custom sort comparator:
function sortCardsWithPriority(cards) { const isMatch = (str, target) => str.toLowerCase() === target.toLowerCase(); return [...cards].sort((a, b) => { const aIsPrimary = isMatch(a, 'Primary'); const bIsPrimary = isMatch(b, 'Primary'); const aIsSecondary = isMatch(a, 'Secondary'); const bIsSecondary = isMatch(b, 'Secondary'); // Primary always takes precedence over everything else if (aIsPrimary && !bIsPrimary) return -1; if (!aIsPrimary && bIsPrimary) return 1; // Secondary comes next, after all Primary entries if (aIsSecondary && !bIsSecondary) return -1; if (!aIsSecondary && bIsSecondary) return 1; // Sort remaining elements case-insensitively return a.toLowerCase().localeCompare(b.toLowerCase()); }); } // Example usage: let Card = ['BBC', 'primary', 'DDC', 'secondary', 'Abccc', 'Primary']; Card = sortCardsWithPriority(Card); console.log(Card); // Output: ['primary', 'Primary', 'secondary', 'Abccc', 'BBC', 'DDC']
Key Details
- Both solutions avoid mutating the original array (we work with copies to keep your source data intact)
- Case insensitivity is handled using
toLowerCase()for all comparisons - The remaining elements are sorted case-insensitively to ensure predictable, consistent ordering
内容的提问来源于stack exchange,提问作者rock rake
相关产品推荐
相关产品推荐

