string.localeCompare

localeCompare() ๋ฉ”์„œ๋“œ๋Š” ์ฐธ์กฐ ๋ฌธ์ž์—ด๊ณผ ๋น„๊ต ํ–ˆ์„ ๋•Œ ์ •๋ ฌ๋œ ์ˆœ์„œ๊ฐ€ ์ธ์ˆ˜๋กœ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด๋ณด๋‹ค ์ „์—์˜ค๋Š”์ง€ ํ›„์— ์˜ค๋Š”์ง€ ํ˜น์€ ๊ฐ™์€ ์ˆœ์„œ์— ๋ฐฐ์น˜๋˜๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์ˆซ์ž๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

const a = 'cat'; // ์†Œ๋ฌธ์ž
const b = 'Cat'; // ๋Œ€๋ฌธ์ž

a.localeCompare(b); // -1
a.localeCompare(b, 'en', {sensitivity: 'base'}); // 0

์ƒˆ locales ์™€ option ์ธ์ˆ˜๋ฅผ ํ†ตํ•ด ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์€ ์ •๋ ฌ ์ˆœ์„œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด๋ฅผ ์ง€์ •, ํ•จ์ˆ˜์˜ ๋™์ž‘์„ ์‚ฌ์šฉ์ž ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ „ locales ์™€ option ์ธ์ˆ˜๋Š” ๋ฌด์‹œ๋œ๋‹ค. locale๊ณผ ์ •๋ ฌ์ˆœ์„œ๋Š” ๊ตฌํ˜„์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค.

Syntax

referenceStr.localeCompare(compareString[, locales, [, options]])

Parameters

compareString

์ฐธ์กฐํ•˜๋Š” ๋ฌธ์ž์—ด๊ณผ ๋น„๊ตํ•˜๋Š” ๋ฌธ์ž์—ด

locales ๊ณผ options

ํ•จ์ˆ˜์˜ ๋™์ž‘์„ ์ปค์Šคํ…€ํ•˜๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์–ธ์–ด๋ฅผ ์ง€์ •, ์ด์ „ locales ์™€ option ์ธ์ˆ˜๋Š” ๋ฌด์‹œ๋œ๋‹ค. locale๊ณผ ์ •๋ ฌ์ˆœ์„œ๋Š” ๊ตฌํ˜„์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค.

์ธ์ž๊ฐ€ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€ Intl.Collator() constructor ์—์„œ ์ž์„ธํžˆ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Return value

์Œ์ˆ˜๋Š” referenceStr์ด compareString๋ณด๋‹ค ์•ž์— ๋ฐฐ์น˜๋˜๋Š” ๊ฒฝ์šฐ,

์–‘์ˆ˜๋Š” compareString ๋‹ค์Œ์— ๋ฐฐ์น˜๋˜๋Š” ๊ฒฝ์šฐ

0์€ ๋™๋“ฑํ•˜๋‹ค.

Description

-1 ๋˜๋Š” 1์˜ ๋ฐ˜ํ™˜๊ฐ’์— ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค!

W3C spec์—์„œ ์Œ์ˆ˜์™€ ์–‘์ˆ˜๋งŒ ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๋Š” ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ๋‹ค์–‘ํ•˜๊ณ  ์ผ๋ถ€ ๋ธŒ๋ผ์šฐ์ €๋Š” -2 ๋˜๋Š” 2๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์‹ฌ์ง€์–ด ๋‹ค๋ฅธ ์Œ์ˆ˜์™€ ์–‘์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

Examples

localeCompare() ์‚ฌ์šฉํ•˜๊ธฐ

'a'.localeCompare('c'); // -2 or -1 ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค
'check'.localeCompare('against'); // 2 or 1 ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค.
'a'.localeCompare('a'); // 0

๋ฐฐ์—ด ์ •๋ ฌ

localeCompare() ๋Š” ๋ฐฐ์—ด์˜ ๋Œ€์†Œ๋ฌธ์ž ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

let items = ['rรฉservรฉ', 'Premier', 'Clichรฉ', 'communiquรฉ', 'cafรฉ', 'Adieu'];
items.sort((a, b) => a.localeCompare(b, 'fr', {ignorePunctuation: true}));
// ["Adieu", "cafรฉ", "Clichรฉ", "communiquรฉ", "Premier", "rรฉservรฉ"]

extended argument์— ๋Œ€ํ•œ ๋ธŒ๋ผ์šฐ์ € ์ง€์› ํ™•์ธํ•˜๊ธฐ

locales์™€ options ๋Š” ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.

i argument๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž˜๋ชป๋œ ์ธ์ž๊ฐ€ ๊ฑฐ๋ถ€๋˜์–ด์•ผ ํ•œ๋‹ค. RangeError ๊ฐ€ ์˜ˆ์™ธ๊ฐ€ ๋˜๋Š”์ง€ ๋ณธ๋‹ค.

function localeCompareSupportsLocales() {
  try {
    'foo'.localeCompare('bar', 'i');
  } catch (e) {
    return e.name === 'RangeError';
  }
  return false;
}

// chrome์—์„œ ์‹คํ–‰ํ–ˆ์„ ๋•Œ๋Š” true ๐Ÿง

locales ์‚ฌ์šฉ

localeCompare() ๋Š” ๊ฒฐ๊ณผ๋Š” ์ œ๊ณต๋˜๋Š” ์–ธ์–ด๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด์˜ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์–ป์œผ๋ ค๋ฉด locales ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์–ธ์–ด๋ฅผ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

'รค'.localeCompare('z', 'de'); // ์Œ์ˆ˜ ๋…์ผ์–ด๋Š” z ๋’ค์— รค๊ฐ€ ์˜จ๋‹ค.
'รค'.localeCompare('z', 'sv'); // ์–‘์ˆ˜ ์Šค์›จ๋ด์–ด๋Š” z ์•ž์— รค๊ฐ€ ์˜จ๋‹ค.

options ์‚ฌ์šฉ

localeCompare()์—์„œ ์ œ๊ณต๋ฐ›์€ ๊ฒฐ๊ณผ๋Š” options ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

// ๋…์ผ์–ด์—์„œ รค๋Š” a๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
'รค'.localeCompare('a', 'de', { sensitivity: 'base' }); // 0
// ์Šค์›จ๋ด์–ด์—์„œ๋Š” รค๋Š” a์™€ ๋ณ„๊ฐœ์ด๋‹ค.
'รค'.localeCompare('a', 'sv', { sensitivity: 'base' }); // ์–‘์ˆ˜

์ˆ˜ ์ •๋ ฌ

// ๊ธฐ๋ณธ ๊ฐ’, "2" > "10" ์•ž์ž๋ฆฌ๋กœ ๊ณ„์‚ฐํ•ด์„œ ?
"2".localeCompare("10"); // 1

// options ์‚ฌ์šฉํ•œ ์ˆ˜ ์ •๋ ฌ
"2".localeCompare("10", undefined, {numeric: true}); // -1

// loales tag๋ฅผ ์‚ฌ์šฉํ•œ ์ˆ˜ ์ •๋ ฌ
"2".localeCompare("10", "en-u-kn-true"); // -1

Performance

ํฐ ๋ฐฐ์—ด ์ •๋ ฌ๊ณผ ๊ฐ™์ด ๋งŽ์€ ์ˆ˜๋กœ๋œ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ๋Š” Intl.Collator ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  Intl.Collator ์—์„œ ์ œ๊ณตํ•˜๋Š” compare ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

Browser compatibility

locales ์™€ options ๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์›น๋ทฐ, ์•ˆ๋“œ๋กœ์ด๋“œ firefox์™€ ์•ˆ๋“œ๋กœ์ด๋“œ opera์—์„œ ์ง€์›๋˜์ง€ ์•Š์Œ

Last updated

Was this helpful?