Pavel
Pavel

Reputation: 1288

strings does not sort correctly in Javascript

I have the same arrays with strings, but they it array does not sort correctly and it is copied from my pc. items is typed in the browser.

https://jsfiddle.net/acc8xf0g/

var items = ["hard", "intermediate", "easy"];

var it = [
  "intermediate",
  "hard",
  "еasy"
];

items.sort(function(a, b) {
  return a.localeCompare(b);
});

it.sort(function(a, b) {
  return a.localeCompare(b);
})

$("#test").html(items.join(" "));
$("#test2").html(it.join(" "));

Upvotes: 0

Views: 88

Answers (2)

Paul
Paul

Reputation: 141829

That is not an ascii e in "еasy" (in your it array).

It is a Cyrillic е: http://www.fileformat.info/info/unicode/char/0435/index.htm

Just delete the 'e' and type it again normally.

Updated fiddle: https://jsfiddle.net/acc8xf0g/2/

Upvotes: 5

Dave Newton
Dave Newton

Reputation: 160191

Look at the actual hex value of the "e" in the it array:

22 68 61 72 64 22 2c 20  22 69 6e 74 65 72 6d 65  |"hard", "interme|
64 69 61 74 65 22 2c 22  65 61 73 79 22 0a        |diate","easy".|


22 69 6e 74 65 72 6d 65  64 69 61 74 65 22 2c 20  |"intermediate", |
22 68 61 72 64 22 2c 20  22 d0 b5 61 73 79 22 0a  |"hard", "..asy".|

Upvotes: 3

Related Questions