使用引号在javascript中对数组进行排序

Ser*_*eev 4 javascript arrays sorting

我有一个阵列

var arr = ['hello','"end" two', 'end one', 'yes', 'abc' ];
Run Code Online (Sandbox Code Playgroud)

我需要按如下所示对其进行排序

// abc,  end one, "end" two, hello, yes
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

Nin*_*olz 5

你可以String#localeCompare选择一个选项.

ignorePunctuation

是否应忽略标点符号.可能的值是true和false; 默认是false.

var array = ['hello', '"end" two', 'end one', 'yes', 'abc'];

array.sort(function (a, b) {
    return a.localeCompare(b, undefined, { ignorePunctuation: true });
});

console.log(array);
Run Code Online (Sandbox Code Playgroud)