Modify rule S2871: Emphasize the necessity of using String.localeCompare to sort arrays of strings (#3576)

* Modify rule S2871: Add exception for arrays of strings

* S2871: Remove the exception mention and emphasize the usage of String.localeCompare
This commit is contained in:
Eric Morand 2024-01-31 17:27:47 +01:00 committed by GitHub
parent d301b56761
commit d2f786adb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -25,7 +25,7 @@ numbers.sort((a, b) => a - b);
console.log(numbers); // Output: [1, 2, 5, 10, 30]
----
Even to sort strings, the default sort order may give unexpected results. Not only does it not support localization, it also doesn't fully support Unicode, as it only considers UTF-16 code units. For example, in the code below, `"eΔ"` is surprisingly before and after `"éΔ"`.
Even to sort strings, the default sort order may give unexpected results. Not only does it not support localization, it also doesn't fully support Unicode, as it only considers UTF-16 code units. For example, in the code below, `"eΔ"` is surprisingly before and after `"éΔ"`. To guarantee that the sorting is reliable and remains as such in the long run, it is necessary to provide a compare function that is both locale and Unicode aware - typically `String.localeCompare`.
[source,javascript]
----