Imagine you have a pile of gems. You could arrange them ONE BY ONE... or use .sort() to put them in order automatically!
.sort() takes every item in an array and rearranges them based on their values. It's perfect for:
How it works:
โข (a, b) => a - b means "smallest first"
โข (a, b) => b - a means "biggest first"
โข The function compares pairs and swaps them until everything is in order!
Remember: In Treasure Tally, you used .reduce() to COMBINE all items into one value. Now with .sort(), you can ORGANIZE items in order!
Can you sort an array of words in alphabetical order? The default .sort() without a function works for strings!
Sort from BIGGEST to smallest using (a, b) => b - a
Sort first, then use .reduce() to find the TOTAL! Sort these numbers, then add them all up.
Sort the array, then find the middle value using index Math.floor(arr.length / 2)
Create a function that finds the median value of ANY sorted array!
.sort() โ Puts array items in order automatically(a, b) => a - b for smallest to biggest(a, b) => b - a for biggest to smallest.sort() works on words without extra code.sort() with .reduce() for powerful data analysis!