1

I have an array of objects:

[  
   {  
      "Accept Credit Cards":"17",
      "Take-Out":"17",
      "Alcohol":"16",
      "Caters":"10",
      "Takes Reservations":"11",
      "Smoking":"0",
      "Dogs Allowed":"1",
      "Outdoor Seating":"12",
      "Coat Check":"0",
      "Waiter Service":"14",
      "Wi-Fi":"10",
      "Good For Groups":"16",
      "Wheelchair Accessible":"13"
   }
]

I want to sort this based on the value. So my result should be something like:

[  
       {  
          "Accept Credit Cards":"17",
          "Take-Out":"17",
          "Alcohol":"16",
          "Good For Groups":"16",
          "Wheelchair Accessible":"13"
          AND SO ON.....

      }
]

How will I do this? I tried a sort. But that just sorts based on the key. Any pointers appreciated.

trincot
  • 211,288
  • 25
  • 175
  • 211
RJP
  • 327
  • 4
  • 16
  • 1
    Use hasOwnProperty to iterate through your object: https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty – meavo Nov 22 '16 at 21:10
  • 2
    There is no order in objects, what you're asking makes no sense. – adeneo Nov 22 '16 at 21:11
  • 3
    What's the purpose of sorting an object? Javascript doesn't care about object property ordering; ordering may vary from implementation to implementation. – RyanZim Nov 22 '16 at 21:11
  • 1
    @GetOffMyLawn Not a duplicate, though related. – RyanZim Nov 22 '16 at 21:12
  • @RyanZim I could just link to this http://stackoverflow.com/questions/20977381/sort-a-json-array-object-using-javascript-by-value, but that also links to the same location. – Get Off My Lawn Nov 22 '16 at 21:14
  • 1
    I'm with @RyanZim on this one. If you're going to sort, it should be an array. The property order of an object shouldn't matter. – Gavin Nov 22 '16 at 21:14
  • This question has nothing to do with JSON. Please read the usage description of the `json` tag. – trincot Nov 22 '16 at 21:18

1 Answers1

3

As RyanZim has mentioned in the comments, there isn't really a purpose to sorting the properties of an object. JavaScript does not guarantee the property order, so you shouldn't rely on them being in order. You could, however, make an array of the properties and sort them based on the value in your object.

const arr = [  
   {  
      "Accept Credit Cards":"17",
      "Take-Out":"17",
      "Alcohol":"16",
      "Caters":"10",
      "Takes Reservations":"11",
      "Smoking":"0",
      "Dogs Allowed":"1",
      "Outdoor Seating":"12",
      "Coat Check":"0",
      "Waiter Service":"14",
      "Wi-Fi":"10",
      "Good For Groups":"16",
      "Wheelchair Accessible":"13"
   }
];

const sorted = Object.keys(arr[0]).sort((a, b) => arr[0][b] - arr[0][a]);

sorted.forEach(x => console.log(x + ': ' + arr[0][x]));

If you want to get even fancier and sort by the value then alphabetically:

const arr = [  
   {  
      "Accept Credit Cards":"17",
      "Take-Out":"17",
      "Alcohol":"16",
      "Caters":"10",
      "Takes Reservations":"11",
      "Smoking":"0",
      "Dogs Allowed":"1",
      "Outdoor Seating":"12",
      "Coat Check":"0",
      "Waiter Service":"14",
      "Wi-Fi":"10",
      "Good For Groups":"16",
      "Wheelchair Accessible":"13"
   }
];

const sorted = Object.keys(arr[0]).sort((a, b) => {
  if (arr[0][a] > arr[0][b]) return -1;
  if (arr[0][a] < arr[0][b]) return 1;
  if (a > b) return 1;
  if (a < b) return -1;
  return 0;
});

sorted.forEach(x => console.log(x + ': ' + arr[0][x]));
Gavin
  • 3,468
  • 1
  • 14
  • 25