js
a.localeCompare(b, locales?, options?)المقارنة بـ< بتعتمد على قيم الـcode units، فكل الحروف الكبيرة قبل الصغيرة. localeCompare بتحترم قواعد اللغة، وبـsensitivity: 'base' بتتجاهل الحالة.
امتى أستخدمها
لما بترتّب أسماء للمستخدم. للـsort بدون comparator الترتيب بالـcode units.
البرهان
js
const words = ['b', 'a', 'C', 'B'];
console.log([...words].sort());
console.log([...words].sort((x, y) => x.localeCompare(y)));
console.log('a'.localeCompare('b') < 0, 'a'.localeCompare('a'));
console.log('a'.localeCompare('A', undefined, { sensitivity: 'base' }));
console.log(['10', '9', '2'].sort((x, y) => x.localeCompare(y, undefined, { numeric: true })));النتيجة
[ 'B', 'C', 'a', 'b' ] [ 'a', 'b', 'B', 'C' ] true 0 0 [ '2', '9', '10' ]