Cara menggunakan mdn javascript string compare
The Show The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request. The new Syntax
ParametersCheck the
Browser compatibility section to see which browsers support the compareString The string against which the referring string is compared
Optional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the co Variant collations for certain locales. Possible values include: "big5han" , "dict" , "direct" , "ducet" , "gb2312" , "phonebk" , "phonetic" , "pinyin" , "reformed" , "searchjl" , "stroke" , "trad" , "unihan" . The "standard" and "search" values are ignored; they are replaced by the options property usage (see below).kn Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are "true" and
"false" . This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence.kf Whether upper case or lower case should sort first. Possible values are "upper" , "lower" , or "false" (use the locale's default). This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence.options
Optional. An object with some or all of the following properties: localeMatcher The locale matching algorithm to use. Possible values are "lookup" and "best fit" ; the default is "best fit" . For information about this option, see the Intl page.usage Whether the comparison is for sorting or for searching for matching strings. Possible values are "sort" and "search" ; the default is "sort" .sensitivity Which differences in the strings should lead to non-zero result values. Possible values are:
The default is ignorePunctuation Whether punctuation should be ignored. Possible values are true and false ; the default is false .numeric Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are true and false ; the
default is false . This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.caseFirst Whether upper case or lower case should sort first. Possible values are "upper" , "lower" , or "false" (use the locale's default); the default is "false" . This option can be set through an options property or through a Unicode extension key;
if both are provided, the options property takes precedence. Implementations are not required to support this property.Return valueA negative number if the reference string occurs before the compare string; positive if the reference string occurs after the compare string; 0 if they are equivalent. DescriptionReturns an integer indicating whether the referenceStr comes before, after or is equivalent to the compareStr.
DO NOT rely on exact return values of -1 or 1. Negative and positive integer results vary between browsers (as well as between browser versions) because the W3C specification only mandates negative and positive values. Some browsers may return -2 or 2 or even some other negative or positive value. ExamplesUsing localeCompare()Sort an array
Check browser support for extended argumentsThe Using localesThe results provided by Using optionsThe results provided by Numeric sortingPerformanceWhen comparing large numbers of strings, such as in sorting large arrays, it is better to create an
Specifications
Browser compatibilityThe compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. Update compatibility data on GitHub
LegendFull support Full support No support No support Compatibility unknown Compatibility unknownSee also
|