如何根据数字和字符串(阿拉伯名称)对对象数组进行排序?



这是我的片段,它的名字是阿拉伯语的(即我想显示沙特阿拉伯的一些数据(

我尝试了本地compare()方法,但不起作用

let arr = [{
"id": "4231a075-ac9d-4c58-b6c2-2d4cf73d72e1",
"name": "النَّشاطُ الثّالِثُ - وَرْشَةُ الْكِتابَةِ 2",
"author": "na",
"countryCode": "SA",
"createdAt": "2019-10-06T07:30:16.770Z",
"pageId": "188"
},
{
"id": "fde7d816-4eb8-4c82-a875-23hsd",
"name": "النَّشاطُ الثّاني - وَرْشَةُ الْكِتابَةِ 1",
"author": "na",
"countryCode": "SA",
"createdAt": "2019-10-06T07:30:16.770Z",
"pageId": "188"
}
]
arr.sort(function(a, b) {
return a.pageId - b.pageId || a.name.localeCompare(b.name, ["ar"]);
});
console.log(arr)

您输入的阿拉伯文本数字似乎不是正确的阿拉伯语编码。现在似乎一切都很好,排序正确。

let arr = [{
"id": "4231a075-ac9d-4c58-b6c2-2d4cf73d72e1",
"name": "2 النَّشاطُ الثّالِثُ - وَرْشَةُ الْكِتابَةِ",
"author": "na",
"countryCode": "SA",
"createdAt": "2019-10-06T07:30:16.770Z",
"pageId": "188"
},
{
"id": "fde7d816-4eb8-4c82-a875-23hsd",
"name": "1 النَّشاطُ الثّاني - وَرْشَةُ الْكِتابَةِ",
"author": "na",
"countryCode": "SA",
"createdAt": "2019-10-06T07:30:16.770Z",
"pageId": "188"
}
]
arr.sort(function(a, b) {
return a.pageId - b.pageId || a.name.localeCompare(b.name, ["ar"]);
});
console.log(arr)

最新更新