Use built-in sort with a custom comparison function
When to useSort by frequency, multi-key sort, lexicographic with twist (largest number)
iterative
Sorting with Custom Criteria
sorting_with_custom_criteriaSort Characters by Frequency
sort_by_frequencyLargest Number from Array
largest_number_from_arrayRelative Sort Array
relative_sort_arrayBuilt-in Sort with Custom Comparator
custom_comparator_sortFetch Items To Display
fetch_items_to_displayReorder Data in Log Files | Upgrading Junction Boxes
reorder_data_in_log_filesTop N Buzzwords
top_n_buzzwords · no slotsGame Events
twitter_oa_game_eventsTop K Frequently Mentioned Keywords
top_k_frequently_mentioned_keywordsINITIALIZE
optional: build frequency map / derive sort key per element
const result = [...people];
const counts = new Map();for (const ch of s) counts.set(ch, (counts.get(ch) ?? 0) + 1);const chars = [...counts.keys()];
const strs = nums.map(String);
const order = new Map();for (let i = 0; i < arr2.length; i++) order.set(arr2[i], i);
// (optional) build sort key or frequency map
const sorted = [...items];
const letterLogs = []; const digitLogs = [];
—
const eventOrder = { G: 0, Y: 1, R: 2, S: 3 };const tagged = [];for (const e of team1Events) tagged.push({ team: team1Name, raw: e });for (const e of team2Events) tagged.push({ team: team2Name, raw: e });
const keywordSet = new Set(keywords.map(w => w.toLowerCase()));const counts = new Map();for (const word of keywordSet) counts.set(word, 0);for (const review of reviews) {const tokens = review.toLowerCase().split(/[^a-z]+/);const seen = new Set();for (const token of tokens) {if (keywordSet.has(token) && !seen.has(token)) {counts.set(token, counts.get(token) + 1);seen.add(token);}}}const entries = [...counts.entries()];
[COMPARATOR]
arr.sort with custom (a, b) comparator
result.sort((a, b) => {if (a.age !== b.age) return a.age - b.age;if (a.name < b.name) return -1;if (a.name > b.name) return 1;return 0;});
chars.sort((a, b) => {const diff = counts.get(b) - counts.get(a);if (diff !== 0) return diff;return a.localeCompare(b);});
strs.sort((a, b) => (b + a).localeCompare(a + b));
return [...arr1].sort((a, b) => {const ai = order.has(a) ? order.get(a) : arr2.length + a;const bi = order.has(b) ? order.get(b) : arr2.length + b;return ai - bi;});
arr.sort((a, b) => { return /* negative: a first, positive: b first */; });
sorted.sort((a, b) => {const av = a[sortParameter]; const bv = b[sortParameter];let cmp = (sortParameter === 0) ? (av < bv ? -1 : av > bv ? 1 : 0) : av - bv;return sortOrder === 0 ? cmp : -cmp;});
for (const log of logs) { /* partition */ }letterLogs.sort((a, b) => {if (a[1] !== b[1]) return a[1] < b[1] ? -1 : 1;return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;});
—
tagged.sort((a, b) => {const partsA = a.raw.split(' ');const partsB = b.raw.split(' ');const [tA1, tA2] = parseTime(partsA[1]);const [tB1, tB2] = parseTime(partsB[1]);if (tA1 !== tB1) return tA1 - tB1;if (tA2 !== tB2) return tA2 - tB2;if (eventOrder[partsA[2]] !== eventOrder[partsB[2]])return eventOrder[partsA[2]] - eventOrder[partsB[2]];if (a.team !== b.team) return a.team.localeCompare(b.team);return partsA[0].localeCompare(partsB[0]);});
entries.sort((a, b) => {if (a[1] !== b[1]) return b[1] - a[1];if (a[0] < b[0]) return -1;if (a[0] > b[0]) return 1;return 0;});
post-process
optional: derive final result from sorted order
// sorted in place — no additional transform needed
const result = chars.map((ch) => ch.repeat(counts.get(ch))).join('');
if (strs[0] === '0') return '0';
// (slice is already the sorted result)
// (optional) derive final answer from sorted array
const start = pageNumber * itemsPerPage;return sorted.slice(start, start + itemsPerPage).map(item => item[0]);
const result = letterLogs.map(e => e[2]);for (const log of digitLogs) result.push(log);
—
// map each tagged event to 'teamName raw-event-string'
const result = [];for (let i = 0; i < Math.min(k, entries.length); i++) {result.push(entries[i][0]);}
RETURN
return result
return result;
return result;
return strs.join('');
return sorted;
return arr;
return result;
return result;
—
return tagged.map((t) => `${t.team} ${t.raw}`);
return result;