1

Here is my snippet which has names in Arabic (i.e I want to show some data for Saudi Arabia)

I have tried local compare() method but doesn't work

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)
Nimantha
  • 5,793
  • 5
  • 23
  • 56
Ramusesan
  • 824
  • 2
  • 11
  • 31

1 Answers1

1

It seems that the numbers you entered with the Arabic text are not proper Arabic encoded. Now seems all fine and sorts properly.

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)
Nimantha
  • 5,793
  • 5
  • 23
  • 56
Mohsen Alyafei
  • 3,731
  • 3
  • 16
  • 34