JavaScript Quiz: Sorting Gotchas

JavaScript's sort() has some famous surprises. This quiz tests the default string ordering, compare functions, in-place sorting vs toSorted(), stable sorts and the comparator bugs that pass a quick test but fail later.

मीडियम 10 सवाल ~6 मिनट पास होने के लिए 80% Node.js 22

मोड चुनें

आप गेस्ट के तौर पर खेल रहे हैं: XP और streak सेव रखने के लिए साइन इन करें।

सैंपल सवाल

सवाल 1 / 10

What does this print?

const nums = [10, 1, 5, 100];
console.log(nums.sort().join(","));
जवाब दिखाएं

C. 1,10,100,5 Without a compare function, sort() converts items to strings and compares them character by character. '100' comes before '5' because '1' is less than '5'.

इस क्विज़ में क्या है

  • Why the default sort() compares items as strings
  • Writing compare functions like (a, b) => a - b
  • sort() changes the array, toSorted() returns a copy
  • Case, localeCompare() and undefined items
  • Sorting objects by a property, and stability

अक्सर पूछे जाने वाले सवाल

क्या अकाउंट ज़रूरी है?

नहीं। कोई भी खेल सकता है। XP, streak, badge और हिस्ट्री सेव रखने के लिए साइन इन करें।

क्या मैं इसे दोबारा दे सकता हूं?

हां। आपका बेस्ट स्कोर सेव रहता है, और हर बार सवाल और जवाब शफ़ल होते हैं।