1

I have a JavaScript object that I am adding to using the .push(), but the problem is that when I push data to the object the object is no longer in order by time values. How can I create this same object but always have it be order by time_going_hour and then by time_going_minutes?

"days": {
    "1": {
        "places": [{
            "url": "/attraction/mauna-kea-summit/19",
                "img_src": "http://trekeffect.images.user_images.s3.amazonaws.com/4_36.jpg",
                "title": "Mauna Kea Summit",
                "time_going_hour": 1,
                "time_going_minutes": 55,
                "duration": "3600",
                "id": 19,
                "type": "attraction",
                "city_id": 6
        }, {
            "url": "/attraction/the-st.-benedict-painted-church/21",
                "img_src": "http://trekeffect.images.user_images.s3.amazonaws.com/4_42.jpg",
                "title": "The St. Benedict Painted Church",
                "time_going_hour": 8,
                "time_going_minutes": 35,
                "duration": "2600",
                "id": 21,
                "type": "attraction",
                "city_id": 6
        }, {
            "url": "/attraction/mauna-kea-summit/19",
                "img_src": "http://trekeffect.images.user_images.s3.amazonaws.com/4_36.jpg",
                "title": "Mauna Kea Summit",
                "time_going_hour": 3,
                "time_going_minutes": 51,
                "duration": "3600",
                "id": 19,
                "type": "attraction",
                "city_id": 6
        }]
    }
Patartics Milán
  • 4,858
  • 4
  • 26
  • 32
Jason Biondo
  • 736
  • 5
  • 16
  • 34

2 Answers2

5

Try

places.sort(function(x, y) {
   return x.time_going_hour - y.time_going_hour || 
          x.time_going_minutes - y.time_going_minutes
})

Array.sort accepts a comparison function. The comparison function takes two arguments (here, x and y) and returns a result that is either:

  • greater than 0 (if x > y),
  • less than 0 (if x < y), or
  • exactly 0 (if x == y).

This compare function first tries to return the difference of x's hour and y's hour (so that if the hour of x is greater than the hour of y, the difference is positive, and otherwise it's negative). If the hour values are equal, then the hour difference is 0, and the right-hand part of the "or" (||) is evaluated (since 0 evaluated in a boolean context is false). This right-hand expression performs the same comparison as before, but with minutes instead of hours.

In short, the comparison function sorts values by hour, and if the hours are the same for any given elements, it sub-sorts them by minutes.

apsillers
  • 112,806
  • 17
  • 235
  • 239
georg
  • 211,518
  • 52
  • 313
  • 390
  • This should work for me. I don't quite understand it, but thanks anyway. – Jason Biondo May 20 '13 at 18:27
  • @JasonBiondo If you look up sorting algorithms, some of them use a comparison operation to shift items backwards or forwards in the array/object. In this case, the subtraction of the hours from each other will yield either a negative or positive number which is then used to push that value forwards or backwards in your array/object. The minutes are then compared for secondary sorting based on that value. – Alex W May 20 '13 at 18:31
  • @JasonBiondo I went ahead and edited in an explanation of thg435's code. – apsillers May 20 '13 at 18:42
  • @apsillers: good job! – georg May 20 '13 at 19:10
1

you can use javascript sort method to sort the array after ever addition/push.

sort(function(a,b){
return (a["time_going_hour"]*60+a["time_going_mnutes"]) - (b["time_going_hour"]*60+b["time_going_mnutes"])
})

apply this function on your places array

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80