๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • Syntax
  • Parameters
  • Return value
  • Description
  • Examples
  • localeCompare() ์‚ฌ์šฉํ•˜๊ธฐ
  • ๋ฐฐ์—ด ์ •๋ ฌ
  • extended argument์— ๋Œ€ํ•œ ๋ธŒ๋ผ์šฐ์ € ์ง€์› ํ™•์ธํ•˜๊ธฐ
  • locales ์‚ฌ์šฉ
  • options ์‚ฌ์šฉ
  • ์ˆ˜ ์ •๋ ฌ
  • Performance
  • Browser compatibility

Was this helpful?

  1. javascript-api

string.localeCompare

Previousstring.trimNextString.indexOf

Last updated 5 years ago

Was this helpful?

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๊ณผ ์ •๋ ฌ์ˆœ์„œ๋Š” ๊ตฌํ˜„์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค.

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

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 ๋Š” ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.

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์—์„œ ์ง€์›๋˜์ง€ ์•Š์Œ

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

Intl.Collator() constructor
RangeError