ควิซ JavaScript: 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, เหรียญตรา และประวัติ

ทำซ้ำได้ไหม?

ได้ ระบบเก็บคะแนนที่ดีที่สุดไว้ และสลับคำถามกับตัวเลือกใหม่ทุกครั้ง