【问题标题】:jQuery Sort nested JSON objects by date?jQuery 按日期对嵌套的 JSON 对象进行排序?
【发布时间】:2016-02-19 15:41:33
【问题描述】:

在我提出这个问题之前,我已经搜索了StackOverflow,但我找不到我的案例的解决方案。 我从多个Facebook Graph API 调用中接收对象(见第一张图片)。 我想做的是按日期(event.start_time)对我的对象进行排序。 是否可以创建额外的数组?

对象的结构在第二张图片中可见。

变量“event”保存所有对象。

更新 我创建了一个全局数组并将所有事件对象推送到它,它现在是一个数组,所以它应该是可排序的。我就是不知道怎么做,请帮忙。

        var arr = []; //global array to sort event objects
    //function is called somewhere
    function makeEvents(ids, infowindow, map, accessToken) {
            var currentTime = dateFormat("date", new Date(), true);
            //console.log(currentTime);
            var display = document.getElementById("all");
            var carousel = document.getElementById("carousel-inner");
             // console.log(ids);

            FB.api('/',
            {ids : ids}, 
            function (pages) {
              if(pages) {
                $.each(pages, function(page_key, page_value) {
                    var id = page_value.id;
                    //display.innerHTML += '<tr>';
                    //console.log(id);
                     //if date search changed, since date search input
                    FB.api('/'+ id +'/events?access_token='+ accessToken +'&since='+ currentTime, function (events) { //&offset=0   &until=2016-02-31
                        //console.log(events.data);

                      if(events) {
                        //console.log(events.data)
                        $.each(events.data, function(events_key, events_value) {
                          FB.api('/'+ events_value.id + '?fields=id,name,cover,description,start_time,place,ticket_uri,picture', function (event) {
                            if(event) {                                 
                             //console.log(event);
                             arr.push(event);//fill array
                              }
                          });
                        });
                      }
                    });
                   // display.innerHTML += '</tr>';
                });
              }
            });  
    }
    console.log(arr);
    arr.sort(function(a, b){
     if(a.start_time < b.start_time)
       return -1;
     if(a.start_time > b.start_time)
       return 1;
     return 0;
    });

这应该不起作用,因为我在数组中的深度不够。

已解决必须使用超时功能,因为数组在完全填充之前排序,这让我觉得它根本没有排序。如果有更好的方法来检查数组是否完成填充,请告诉我:)

   setTimeout(function(){
      arr = arr.sort(function (a, b) {
        return a.name.localeCompare( b.name );
      });
      console.log(arr);
    }, 5000);

【问题讨论】:

  • 您还没有阵列?如果你这样做了,那么这对于内置的排序和比较器功能来说应该是微不足道的。

标签: jquery json sorting date object


【解决方案1】:

你不能像这样使用 JavaScript 排序功能吗? -

arr.sort(function(a, b){
    if(a.start_time < b.start_time)
        return -1;
    if(a.start_time > b.start_time)
        return 1;
    return 0;
});

http://www.w3schools.com/jsref/jsref_sort.asp

jsfiddle:https://jsfiddle.net/mhccnpkp/

更新:起初我认为这只是您遇到的一个简单的排序问题,但现在我看到您正在尝试从ajax 调用列表中对结果进行排序。在这种情况下,您必须等待所有调用完成加载,而主要的事实是,不能保证每个调用都会按顺序结束。所以我建议你使用信号量来确保所有调用都结束,然后调用 sort 来对你的数组进行排序。像这样的 -

var arr = []; //global array to sort event objects
//function is called somewhere
var semaphore = 0; //the semaphore
function makeEvents(ids, infowindow, map, accessToken) {
    ....

    FB.api('/',
    {ids : ids}, 
    function (pages) {
      if(pages) {
        $.each(pages, function(page_key, page_value) {

            semaphore += 1; // increase the semaphore for each call
            ....
            FB.api('/'+ id +'/events?access_token='+ accessToken +'&since='+ currentTime, function (events) { //&offset=0   &until=2016-02-31
                //console.log(events.data);

              semaphore -= 1; //reduce semaphore count, sicne this call is completed 
              .....

                  semaphore += 1; // increase the semaphore for next call

                  FB.api('/'+ events_value.id + '?fields=id,name,cover,description,start_time,place,ticket_uri,picture', function (event) {

                    ....

                    semaphore -= 1; //reduce semaphore count, sicne this call is completed 

                    //check if all calls completed
                    if(semaphore == 0){
                        arr.sort(function(a, b){
                            if(a.start_time < b.start_time)
                                return -1;
                            if(a.start_time > b.start_time)
                                return 1;
                            return 0;
                        });
                    }                   
                  });

                });
              }

            });
           ...
        });
      }
    });  
}
console.log(arr);

顺便说一句,这是非常基本的检查。这只有在所有调用都成功时才有效。因此,您可能还想添加一些检查和错误处理程序,并减少错误处理程序中的信号量。否则,一次不成功的调用将使系统无限期地等待。

【讨论】:

  • 返回错误:event.sort 不是函数,.sort 只适用于我认为的数组?
  • 确保events 是一个数组。我添加了一个 jsfiddle
  • 事件或事件?这是 2 个不同的对象。
  • 代码是对任意数组进行排序。只需在那里使用适当的变量。不是很明显吗?
  • 我现在有一个数组要排序,但它是一个充满对象的数组,它会产生类似 arr.object.sort(function(a,b){ ... }); ??还是我应该先循环它?
【解决方案2】:
var arr = []; //global array to sort event objects
function makeEvents(ids, infowindow, map, accessToken) {
        var currentTime = dateFormat("date", new Date(), true);
        //console.log(currentTime);
        var display = document.getElementById("all");
        var carousel = document.getElementById("carousel-inner");
        // console.log(ids);

        FB.api('/',
        {ids : ids}, 
        function (pages) {
            if(pages) {
                $.each(pages, function(page_key, page_value) {
                    var id = page_value.id;
                    //display.innerHTML += '<tr>';
                    //console.log(id);
                    //if date search changed, since date search input
                    FB.api('/'+ id +'/events?access_token='+ accessToken +'&since='+ currentTime, function (events) { //&offset=0   &until=2016-02-31
                        //console.log(events.data);

                        if(events) {
                            //console.log(events.data)
                            $.each(events.data, function(events_key, events_value) {
                                FB.api('/'+ events_value.id + '?fields=id,name,cover,description,start_time,place,ticket_uri,picture', function (event) {
                                    if(event) {                                 
                                        arr.push(event);
                                        shortArray();
                                    }
                                });
                            });
                        }
                    });
                    // display.innerHTML += '</tr>';
                });
            }
        });  
    }


    function shortArray(){
        arr.sort(function(a, b){
            if(a.start_time < b.start_time)
                return -1;
            if(a.start_time > b.start_time)
                return 1;
            return 0;
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2016-07-09
    • 2014-05-12
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多