Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting non-Latin strings does not work

I have an issues with sorting on non-Latin strings of array object. I have used this _.sortBy(object, 'name') in my code to sort on name. Here is my array for Persian text:

[['id':1,'name':'ب'],['id':2,'name':'ج'],['id':3,'name':'اما']]

And output should be like this after sorting:

['id':3,'name':'اما'],['id':1,'name':'ب'],['id':2,'name':'ج']

But it's not giving this output. Does anyone have idea about this?

like image 353
Hitesh S Avatar asked May 24 '26 05:05

Hitesh S


1 Answers

First you need to correct your Array. It has an invalid format.

var a = [{'id':1,'name':'ب'},{'id':2,'name':'ج'},{'id':3,'name':'اما'}];

a.sort(function(a, b) {

  if (a.name > b.name) {

    return true;
  }
});

Input: [{'id':1,'name':'ب'},{'id':2,'name':'ج'},{'id':3,'name':'اما'}]

Output: [{'id':3,'name':'اما'},{'id':1,'name':'ب'},{'id':2,'name':'ج'}]

like image 80
Ali Mamedov Avatar answered May 26 '26 18:05

Ali Mamedov