← Home CP Β· 10-12 πŸͺ™

Treasure Sorter

72
Day 1 πŸ”₯
🎯 Today's Missions
πŸͺ™ πŸ“œ πŸ‘‘ πŸ†
πŸͺ™
Treasure Island Universe
Treasure Sorter
In Treasure Filter you FOUND treasures with .filter(). Today put the whole vault IN ORDER with .sort()! You write the rules with a comparator: (a, b) => a.value - b.value sorts cheapest β†’ priciest. Flip it to b.value - a.value for priciest first! And beware the Number Trap... πŸͺ€

πŸͺ™ What we're MAKING today: My Treasure Sorter

In Dragon Array you learned that arrays are ordered lists with index [0], [1], [2]... In Treasure Filter you kept only the treasures you wanted. But the vault shelf is still a MESS! What if the cheapest should come first? Or the priciest? Or names A β†’ Z? That's a job for .sort() β€” and today YOU write its rules! πŸͺ™

What is a comparator? It's a tiny function you hand to sort() that compares two items, a and b. Return a negative number β†’ a goes first. Return a positive number β†’ b goes first. (a, b) => a.value - b.value means "smaller values first" (ascending). Flip the subtraction β†’ descending! For names, use a.name.localeCompare(b.name) β€” it knows the alphabet! πŸ”€

πŸ”— Last time in Treasure Filter you chained filter().map() to find and transform treasures. Today you'll put results in ORDER with sort() β€” and chain it with slice(0, 3) to build a Top 3 leaderboard! Same vault, new superpower! πŸ†
treasure-sorter.js JavaScript

    
πŸ’‘ Comparators return: βˆ’ number β†’ a first, + number β†’ b first sort = you write the rules
vault shelf LIVE
πŸ“š Shown
8
πŸ’° Priciest
β€”
πŸͺ™ Cheapest
β€”
πŸ’Ž The Vault (array)
πŸͺ™ vault after .sort() β€” read it left to right: [0], [1], [2]...
πŸͺ€ MISSION: The Number Trap β€” try plain .sort() on numbers!
var scores = [80, 9, 100];
scores.sort(); // no comparator... β†’ ❓❓❓
scores.sort((a, b) => a - b); β†’ [9, 80, 100] βœ“
😱 Plain .sort() compares items as TEXT: "100" < "80" < "9" (just like "1" < "8" < "9" in the dictionary)! That's why YOU must always hand it a comparator like (a, b) => a - b for numbers. πŸͺ€ Dodged!
Step 1 / 5
πŸ”‘ Sort by KEY (which property?)
🧭 DIRECTION (which way?)
πŸ† Top 3 only (vault.slice(0, 3))
OFF β€” showing the whole vault
πŸ“‹ Your 5 Sorting Challenges
0 of 5 challenges complete
πŸͺ™
You Mastered Treasure Sorter!
You wrote comparator functions that tell sort() EXACTLY how to order your array β€” by value, by name, up or down β€” and dodged the Number Trap! Ordering data is what every leaderboard, high-score list, and price filter really does! πŸ†

πŸ’« What You Learned

  • .sort(fn) orders your array using rules YOU write β€” a comparator
  • (a, b) => a - b β†’ ascending Β· (a, b) => b - a β†’ descending
  • πŸͺ€ Plain .sort() compares TEXT: [80, 9, 100] sorts to [100, 80, 9]!
  • Sort objects by any property: vault.sort((a,b) => a.value - b.value)
  • Names use a.name.localeCompare(b.name) β€” it knows A β†’ Z
  • Chain it! vault.sort(...).slice(0, 3) builds a Top 3 leaderboard! πŸ†
πŸͺ™ Your Creation
My Treasure Sorter
πŸ” Come Back Tomorrow!
Next up: Super Search β€” you can filter, transform, and sort... but what about finding ONE exact treasure? .find() returns the FIRST match and .includes() checks if it's in the vault at all! πŸ•΅οΈ
🏠 Home
Nice! πŸŽ‰