字符串在Javascript中无法正确排序

时间:2015-12-18 18:55:22

标签: javascript

我有相同的数组和字符串,但它们it数组没有正确排序,它是从我的电脑上复制的。在浏览器中输入items

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(" "));

2 个答案:

答案 0 :(得分:5)

这不是“еasy”中的ascii e(在it数组中)。

这是西里尔文е: http://www.fileformat.info/info/unicode/char/0435/index.htm

只需删除'e'并再次正常输入。

更新了小提琴:https://jsfiddle.net/acc8xf0g/2/

答案 1 :(得分:3)

查看it数组中“e”的实际十六进制值:

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".|