2

Here is my array and i am trying to sort it. But it is not working as expected. i want to sort the name as descending.. How can i do it ?

var d = '{"success":"1","message":[{"_id":"591b39df358f1d1f843231d1","area":"chennai","food":"idly","name":"saravana bavan","__v":0},{"_id":"591b39e0358f1d1f843231d2","area":"Dindigul","food":"Dosa","name":"Kattu Briyani","__v":0},{"_id":"591b39df358f1d1f843231d4","area":"Tirupur","food":"Poori","name":"French Loaf","__v":0}]}';
console.log(d);
var results = jQuery.parseJSON(d);
console.log(results.message);
results.message.sort(function(a, b) {
  return b.name- a.name;
});
console.log(results.message);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Here is my Fiddle

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
SA__
  • 1,782
  • 3
  • 20
  • 41

2 Answers2

1

You can't perform mathematical operations on strings.

results.message.sort(function(a, b) {
    if (b.name > a.name) { return 1 }
    else if (b.name < a.name) { return -1 }
    else { return 0 }
});

As @bergi mentioned in a comment below. This explains the problem in depth.

Community
  • 1
  • 1
Will Reese
  • 2,801
  • 2
  • 15
  • 27
  • 1
    is it just me or this answer is from the link given by @Bergi – guradio May 17 '17 at 02:54
  • Just checked. Its basically the same thing, but there's not really another way to do this is there? I'll add the link to my answer. – Will Reese May 17 '17 at 02:56
  • How about `return b.name.localeCompare(a.name)` – Phil May 17 '17 at 02:57
  • 1
    @guradio Na, I didn't do anything with names in my answer there (which specifically explains the problem of never returning negative values). Of course the correct approach is always the same :-) – Bergi May 17 '17 at 02:57
  • @Phil Better not, that treats special characters differently – Bergi May 17 '17 at 02:58
-1

just change your comparison function as follows:

var d = '{"success":"1","message":[{"_id":"591b39df358f1d1f843231d1","area":"chennai","food":"idly","name":"saravana bavan","__v":0},{"_id":"591b39e0358f1d1f843231d2","area":"Dindigul","food":"Dosa","name":"Kattu Briyani","__v":0},{"_id":"591b39df358f1d1f843231d4","area":"Tirupur","food":"Poori","name":"French Loaf","__v":0}]}';
console.log(d);
var results = jQuery.parseJSON(d);
console.log(results.message);
results.message.sort(function(a, b) {
    return b.name.toLowerCase() > a.name.toLowerCase() ? 1 : -1;
});
console.log(results.message);
am05mhz
  • 2,727
  • 2
  • 23
  • 37
  • [Don't forget to return `0` when appropriate!](http://stackoverflow.com/a/20892652/1048572) – Bergi May 17 '17 at 02:59
  • @Bergi it should be slower right?, but i'm just curious, have someone benchmark it? how much slower is a 'swap' compared to an 'extra if' statement? – am05mhz May 17 '17 at 03:12
  • I don't care about speed when correctness is impaired. Not returning `0` does not work with duplicate values in the sorted array. – Bergi May 17 '17 at 03:16
  • @Bergi in your linked answer, the correctness depends on the algorithm used, but there is no problem with it, even if they got swapped, the values are the same, so it does not result in an incorrect result, only an excess swapping, but that is comparable to an excess if statement – am05mhz May 17 '17 at 03:29