-4

I have this object in Javascript:

[
{
    "id": "60001",
    "name": "NORTH COLORADO MEDICAL CENTER",
    "friendly_name": "Kidney Failure - w/o CC MCC",
    "address": "1801 16TH STREET",
    "state": "CO",
    "zip": "80631",
    "service_count": "14",
    "charges": "18,381",
    "payments": "4,339",
    "distance": "49.920"
},
{
    "id": "60031",
    "name": "CENTURA HEALTH-PENROSE ST FRANCIS HEALTH SERVICES",
    "friendly_name": "Kidney Failure - w/o CC MCC",
    "address": "2222 N NEVADA AVE",
    "state": "CO",
    "zip": "80907",
    "service_count": "21",
    "charges": "18,512",
    "payments": "3,801",
    "distance": "60.961"
}
]

By default the object is ordered by "id". I want to take that Object as it is now, and sort it by the field "name" in ascending order instead of "id". Check Below to see how I want to the results to be displayed.

[
{
    "id": "60031",
    "name": "CENTURA HEALTH-PENROSE ST FRANCIS HEALTH SERVICES",
    "friendly_name": "Kidney Failure - w/o CC MCC",
    "address": "2222 N NEVADA AVE",
    "state": "CO",
    "zip": "80907",
    "service_count": "21",
    "charges": "18,512",
    "payments": "3,801",
    "distance": "60.961"
},
{
    "id": "60001",
    "name": "NORTH COLORADO MEDICAL CENTER",
    "friendly_name": "Kidney Failure - w/o CC MCC",
    "address": "1801 16TH STREET",
    "state": "CO",
    "zip": "80631",
    "service_count": "14",
    "charges": "18,381",
    "payments": "4,339",
    "distance": "49.920"
}
]
Wassim Taher
  • 966
  • 1
  • 9
  • 26
  • 1
    FYI your problem has nothing to do with JSON. Once you parsed the data, you will have an array of objects. *How* you got the data is not relevant to the problem. – Felix Kling Aug 21 '13 at 09:23

2 Answers2

3

Assuming your resulting array is called data, it would look something like this:

data.sort(function(a,b){
   if (a.name > b.name) return 1;
   return -1;
});
John Green
  • 13,241
  • 3
  • 29
  • 51
  • 2
    You have to return `0` too. – Felix Kling Aug 21 '13 at 09:22
  • Yes thanks that works, but when I try it I can see the data sorted by name, but the other information didn't move! Therefore, address, state, zip, etc... are now wrong. – Wassim Taher Aug 21 '13 at 09:24
  • @WassimTaher That's simply not the way sort works. You've got something else wrong. – John Green Aug 21 '13 at 09:25
  • @JohnGreen Hey John it's working now. Thanks a lot. Do you have any idea why people are voting down on my question? – Wassim Taher Aug 21 '13 at 09:33
  • 1
    @Wassim: I assume because haven't shown any effort to solve the problem yourself. Simply [searching SO for `[javascript] sort array of objects`](http://stackoverflow.com/search?q=[javascript]+sort+array+of+objects) would have led you to solutions. – Felix Kling Aug 21 '13 at 09:34
  • @JohnGreen Trust me I did. I even tried this yesterday and it didn't work, so I thought maybe there's something else I am missing. Do you think I should remove the question? I feel like people are attacking me for the question! – Wassim Taher Aug 21 '13 at 09:37
  • 1
    @Wassim: First of all, I answered to your comment, not John. Second, if you have code that didn't work as expected, then post it! We expect questions to *show* what was tried and to explain what didn't work as expected. But before you ask, search on Google and here on SO. As you can see in my previous comment, this question was asked before. And no, don't delete your question. – Felix Kling Aug 21 '13 at 09:44
  • 1
    @FelixKling thanks for your friendly message and advice. I totally appreciate it. Maybe I did a mistake and we all do, so kindly accept my apology. When I posted the question in first place I didn't know it will go that far. You are right the next time I should mention that I tried. I truly respect the Stack Overflow community, and if I have found a solution I would never post a question. I even flagged my question as duplicate of another question and reported that to the moderators. Thanks once again and I am sorry if I did a mistake. – Wassim Taher Aug 21 '13 at 09:52
0

The previous post isn't correct, because 0 is never returned. This will work for your case.

yourData.sort(function(a, b){
    if (a.name > b.name) {
         return 1
    }
    if (a.name < b.name) {
         return -1
    }
    else {
         return 0
    }
});
sla55er
  • 791
  • 1
  • 8
  • 16