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.

7
  • 1
    Use hasOwnProperty to iterate through your object: developer.mozilla.org/nl/docs/Web/JavaScript/Reference/… Commented Nov 22, 2016 at 21:10
  • 2
    There is no order in objects, what you're asking makes no sense. Commented Nov 22, 2016 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. Commented Nov 22, 2016 at 21:11
  • 1
    @GetOffMyLawn Not a duplicate, though related. Commented Nov 22, 2016 at 21:12
  • 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. Commented Nov 22, 2016 at 21:14

1 Answer 1

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]));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.