From 9b38150402541d62ad0ac6a1c310a79bd032d5df Mon Sep 17 00:00:00 2001 From: ASellstone Date: Fri, 13 Jun 2025 13:15:44 +0200 Subject: [PATCH 1/4] Initial commit --- .../terms/localeCompare/localeCompare.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 content/javascript/concepts/strings/terms/localeCompare/localeCompare.md diff --git a/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md new file mode 100644 index 00000000000..fe0edd5dbbf --- /dev/null +++ b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md @@ -0,0 +1,44 @@ +--- +Title: 'The Title' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed +Description: 'A brief description.' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews) +Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! + - 'A subject name' + - 'A second subject name' + - 'An nth subject name' +Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! + - 'A tag' + - 'A second tag' + - 'An nth tag' +CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first + - 'learn-example-course' + - 'paths/example-path' +--- + +[A brief definition - make sure first mention of term is in **bold**.] + +## Syntax + +[Text, code, images, parameters, etc. about the syntax] + +## Example + +[Text, code, images, etc. about example 1] + +## Codebyte Example (if applicable) + +We can currently support: + +- Python +- JavaScript +- Ruby +- C++ +- C# +- Go +- PHP + +See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! + +```codebyte/js +# Example runnable code block. +console.log('Hello, World!'); +``` From baf9355da2af6b0bac269f4add088bd4b0540e59 Mon Sep 17 00:00:00 2001 From: ASellstone Date: Wed, 18 Jun 2025 11:49:34 +0200 Subject: [PATCH 2/4] Finished metadata and brief description --- .../terms/localeCompare/localeCompare.md | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md index fe0edd5dbbf..0621a7a8dcd 100644 --- a/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md +++ b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md @@ -1,20 +1,19 @@ --- -Title: 'The Title' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed -Description: 'A brief description.' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews) -Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! - - 'A subject name' - - 'A second subject name' - - 'An nth subject name' -Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! - - 'A tag' - - 'A second tag' - - 'An nth tag' -CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first - - 'learn-example-course' - - 'paths/example-path' +Title: '.localeCompare()' +Description: 'Determines whether a string comes before, after, or is equal to another string in alphabetical order.' +Subjects: + - 'Web Development' + - 'Computer Science' +Tags: + - 'Comparison' + - 'Methods' + - 'Strings' +CatalogContent: + - 'introduction-to-javascript' + - 'paths/front-end-engineer-career-path' --- -[A brief definition - make sure first mention of term is in **bold**.] +The string method **`.localeCompare()`** returns a number indicating whether the string it operates on comes before, after, or at the same position as the string given as an argument in alphabetical order. The method can take a `locale` as an argument to follow the alphabetical order of a specific language. ## Syntax From 18707083c9f93ce82c6b557db1ad460a3967667f Mon Sep 17 00:00:00 2001 From: ASellstone Date: Wed, 18 Jun 2025 13:02:02 +0200 Subject: [PATCH 3/4] Added to syntax section --- .../strings/terms/localeCompare/localeCompare.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md index 0621a7a8dcd..39e9012753e 100644 --- a/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md +++ b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md @@ -17,7 +17,19 @@ The string method **`.localeCompare()`** returns a number indicating whether the ## Syntax -[Text, code, images, parameters, etc. about the syntax] +```pseudo +string.localeCompare(compareString, locales, options) +``` + +**Parameters:** + +- `compareString`: The string that the reference `string` is compared with. +- `locales` (optional): A string with a language code according to the BCP 47 standard (like "de", fr", "sv"), or an array with multiple strings of such language codes. +- `options` (optional): An object that changes the behavior of the comparison. The `options` are defined by the `Intl.Collator` constructor. + +**Return value:** + +A number indicating the sort order: `-1` if `string` comes before `compareString`, `1` if `string` comes after `compareString`, or `0` if the `string` and `compareString` are at the same position. ## Example From 1fa9a163c7f593d376718241f3bfa0657ba96566 Mon Sep 17 00:00:00 2001 From: ASellstone Date: Sun, 22 Jun 2025 00:29:21 +0200 Subject: [PATCH 4/4] Finished first draft --- .../terms/localeCompare/localeCompare.md | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md index 39e9012753e..5c2e3a15c71 100644 --- a/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md +++ b/content/javascript/concepts/strings/terms/localeCompare/localeCompare.md @@ -1,6 +1,6 @@ --- Title: '.localeCompare()' -Description: 'Determines whether a string comes before, after, or is equal to another string in alphabetical order.' +Description: 'Determines whether a string comes before, after, or at the same position as another string in alphabetical order.' Subjects: - 'Web Development' - 'Computer Science' @@ -13,7 +13,7 @@ CatalogContent: - 'paths/front-end-engineer-career-path' --- -The string method **`.localeCompare()`** returns a number indicating whether the string it operates on comes before, after, or at the same position as the string given as an argument in alphabetical order. The method can take a `locale` as an argument to follow the alphabetical order of a specific language. +The string method **`.localeCompare()`** returns a number indicating whether the reference string it operates on comes before, after, or at the same position as the given string in alphabetical order. The method can take `locales` as an argument to follow the alphabetical order of specific languages. ## Syntax @@ -23,33 +23,50 @@ string.localeCompare(compareString, locales, options) **Parameters:** -- `compareString`: The string that the reference `string` is compared with. -- `locales` (optional): A string with a language code according to the BCP 47 standard (like "de", fr", "sv"), or an array with multiple strings of such language codes. -- `options` (optional): An object that changes the behavior of the comparison. The `options` are defined by the `Intl.Collator` constructor. +- `compareString`: The string that the reference string is compared with. +- `locales` (optional): A string with a language code according to the BCP 47 standard (like "de," "fr," "sv"), or an array with multiple strings of such language codes. +- `options` (optional): An object that changes the behavior of the comparison. The `options` that can be used are defined by the `Intl.Collator` constructor. **Return value:** -A number indicating the sort order: `-1` if `string` comes before `compareString`, `1` if `string` comes after `compareString`, or `0` if the `string` and `compareString` are at the same position. +A number indicating the sort order: + +- a negative number (for example, `-1`) if the reference string comes before `compareString`. +- a positive number (for example, `1`) if the reference string comes after `compareString`. +- `0` if the reference string and `compareString` are at the same position. + +The exact positive and negative numbers that `.localeCompare()` returns may vary between browsers. ## Example -[Text, code, images, etc. about example 1] +This example shows how to use `.localeCompare()` to get the alphabetical order of a string compared to a reference string: -## Codebyte Example (if applicable) +```js +const comparison1 = 'boat'.localeCompare('car'); +const comparison2 = 'boat'.localeCompare('airplane'); +const comparison3 = 'boat'.localeCompare('BOAT', 'en', { sensitivity: 'base' }); // option argument to make the comparison case insensitive + +console.log(`"boat" compared with "car": ${comparison1}`); +console.log(`"boat" compared with "airplane": ${comparison2}`); +console.log(`"boat" compared with "BOAT": ${comparison3}`); +``` + +The output of this code is: + +```shell +"boat" compared with "car": -1 +"boat" compared with "airplane": 1 +"boat" compared with "BOAT": 0 +``` -We can currently support: +## Codebyte Example -- Python -- JavaScript -- Ruby -- C++ -- C# -- Go -- PHP +Run the following code to understand how `.localeCompare()` works with different `locales`: -See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! +```codebyte/javascript +const germanComparison = "z".localeCompare("ä", "de"); +const swedishComparison = "z".localeCompare("ä", "sv"); -```codebyte/js -# Example runnable code block. -console.log('Hello, World!'); +console.log(`German locale: ${germanComparison}`); +console.log(`Swedish locale: ${swedishComparison}`); ```