关于sort方法返回单元素Map及10条目Map排序的技术问询
sortFeaturesPerPriority Returns Single-Element Map Instead of 10 Entries Let's break down why your sorted map is only returning one element, and walk through fixes for the most likely causes:
First, let's recap your code flow for context:
const featuresMap = {}; for (const feature of features) { featuresMap[feature.getName()] = feature.getPriority(); logger.info(': ++++++featuresMap ', featuresMap, '++++++'); } const sortedFeaturesPerPriority = this.sortFeaturesPerPriority(featuresPrioritiesTags); logger.info('[delayOrchestration]: featuresPrioritiesTagsSORTED : ', sortedFeaturesPerPriority);
1. You're passing the wrong variable to the sort method
This is a super common slip-up: you're building featuresMap with 10 entries, but passing featuresPrioritiesTags to sortFeaturesPerPriority. If featuresPrioritiesTags is a different object (maybe a single-entry map/object by accident), that's exactly why you're getting a single element back.
Fix: Swap the parameter to use the map you actually built:
const sortedFeaturesPerPriority = this.sortFeaturesPerPriority(featuresMap);
2. The sortFeaturesPerPriority method has broken logic
Most likely, the internal implementation of your sort method is filtering out entries or grouping everything into a single bucket. For example:
- It might only return the highest/lowest priority group instead of all groups
- It could have a bug where it's assigning every feature to the same priority key
- It might be using a
filterthat accidentally removes 9 out of 10 entries
Example of a broken implementation:
// This only returns the first priority group it finds sortFeaturesPerPriority(map) { const sorted = new Map(); const firstPriority = Object.values(map)[0]; sorted.set(firstPriority, Object.keys(map).filter(key => map[key] === firstPriority)); return sorted; }
Fix: Rewrite the method to properly group all features by priority (and sort if needed):
sortFeaturesPerPriority(featureMap) { const priorityGroups = new Map(); // Loop through every entry in your feature map for (const [featureName, priority] of Object.entries(featureMap)) { // Initialize an array for the priority if it doesn't exist if (!priorityGroups.has(priority)) { priorityGroups.set(priority, []); } // Add the feature to its priority group priorityGroups.get(priority).push(featureName); } // Optional: Sort the priority groups (e.g., highest to lowest) return new Map([...priorityGroups.entries()].sort((a, b) => b[0] - a[0])); }
3. All 10 features have the exact same priority
You said your featuresMap has 10 entries, but if every single one of those entries has the same priority value, grouping them by priority will naturally result in a single map entry. That's not a bug—it's expected behavior!
Fix: Verify the priority values in your featuresMap with a detailed log:
logger.info('Full featuresMap priorities:', Object.entries(featuresMap).map(([name, priority]) => `${name}: ${priority}`));
If all priorities are identical, check the feature.getPriority() method to make sure it's returning unique values where expected.
4. Your logger is truncating the output
Sometimes logging libraries truncate large objects, making it look like you only have one entry when the map actually has more.
Fix: Log the map's size and full entries explicitly to confirm:
logger.info('Sorted map size:', sortedFeaturesPerPriority.size); logger.info('Full sorted entries:', [...sortedFeaturesPerPriority.entries()]);
If the size is 10 but the log looks truncated, adjust your logger settings to show full objects.
内容的提问来源于stack exchange,提问作者Amrmsmb

