Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按小时对对象数组排序_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript 按小时对对象数组排序

Javascript 按小时对对象数组排序,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有一个带有hourfc\u start\u time属性的对象数组,我想按此属性对其进行排序。我试过了,但不管用。 这是对象结构: Object { id="540-events", local_id=540, title="Mas Flow", fc_start_time="12:30 pm" }, Object { id="531-events", local_id=53

我有一个带有hourfc\u start\u time属性的对象数组,我想按此属性对其进行排序。我试过了,但不管用。 这是对象结构:

Object { 
         id="540-events", 
         local_id=540, 
         title="Mas Flow", 
         fc_start_time="12:30 pm"
},
Object { 
         id="531-events", 
         local_id=531, 
         title="Slowly", 
         fc_start_time="04:30 pm"
},
Object { 
         id="531-events", 
         local_id=531, 
         title="Slowly", 
         fc_start_time="08:30 am"
}

提前感谢。

根据与示例相同的时间输入尝试此比较功能。它在时间值的浮动符号上进行比较

var input = [{hour:1, minutes:10},{hour:4, minutes: 1}, ...];
input.sort(function (a, b)
{
    // compare hours first
    if (a.hour < b.hour) return -1;
    if (a.hour > b.hour) return 1;

    // else a.hour === b.hour, so compare minutes to break the tie
    if (a.minute < b.minute) return -1;
    if (a.minute > b.minute) return 1;

    // couldn't break the tie
    return 0;
});
<script>
    var objs = [
        {id:"540-events",local_id:540,title:"Mas Flow",fc_start_time:"12:30 pm"},
        {id:"540-events",local_id:540,title:"Mas Flow",fc_start_time:"10:30 pm"},
        {id:"540-events",local_id:540,title:"Mas Flow",fc_start_time:"12:30 pm"},
        {id:"540-events",local_id:540,title:"Mas Flow",fc_start_time:"14:30 pm"},
        {id:"540-events",local_id:540,title:"Mas Flow",fc_start_time:"09:30 pm"}
    ]       
    function compare(a,b) {
        var time1 = parseFloat(a.fc_start_time.replace(':','.').replace(/[^\d.-]/g, ''));
        var time2 = parseFloat(b.fc_start_time.replace(':','.').replace(/[^\d.-]/g, ''));
        if(a.fc_start_time.match(/.*pm/)) time1 += 12; if(b.fc_start_time.match(/.*pm/)) time2 += 12;
        if (time1 < time2) return -1;
        if (time1 > time2) return 1;
        return 0;
    }   
    objs.sort(compare);
    console.log(objs);      
</script>

试试这个。你必须在你的时间里考虑AM和PM。

<!doctype html>
</html>
    <head>
        <meta charset="utf-8">      
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                // The data
                var data = [
                   { id:"540-events", local_id:540, title:"Mas Flow", fc_start_time:"12:30 pm"}, 
                   { id:"531-events", local_id:531, title:"Slowly", fc_start_time:"04:30 pm"}, 
                   { id:"545-events", local_id:545, title:"Mas Flow 2", fc_start_time:"03:30 am"}
                ]
                // Sort values
                data.sort(function(a,b){
                    var aValue = new Number(a.fc_start_time.replace(/\d*/g,""));
                    var bValue = new Number(b.fc_start_time.replace(/\d*/g,""));
                    if( aTime.match(/.*pm/) ){
                        aValue + 12;
                    }
                    return aValue - bValue;    
                });
                // Show values
                for( var i = 0; i < data.length; i++){
                    $("ul").append("<li>"+data[i].fc_start_time+"</li>");
                }
        });
        </script>
    </head>
    <body>
        <ul>        
        </ul>
    </body>
</html>

日期和时间,嗯?我讨厌他们。让我们试着弄明白这一点

如何对数组进行排序?在…的帮助下。默认情况下,此方法对字符串进行排序,但我们需要自定义比较函数

我们如何比较字符串中的时间?我们可以使用Date对象,解析时间,从1970年开始计算毫秒,然后比较它们。但我们这里有am pm格式。所以,要使用Date.parse方法,我们需要首先使用或使用库

现在我们可以在array.sort比较函数中使用它。我们只是比较两个数字,1411129800000?1411132800000,决定哪个更大并对数组进行排序

function compare(a,b) {

  var atime = Date.parse('9/19/2014 ' + convertTo24Hour(a.time));
  var btime = Date.parse('9/19/2014 ' + convertTo24Hour(b.time));

  if (atime < btime) {
     return -1;
  }

  if (atime > btime) {
    return 1;
  }

  return 0;
}
我们到底得到了什么:

使用array.sort compare函数对元素进行排序 将时间转换为数字,以便我们能够正确比较它们 为此,请将12h时间格式转换为24,并使用Date.parse
这是一个JSFIDLE游戏-

可能是Apsiller的复制品,是同一个概念,我在研究时没有发现它。然而,这种解决方案不适用于我的情况。例如,“上午11:00”和“下午11:00”非常相似,都将在下午12:00之前。非常感谢,这项工作做得非常好。有一件事,如果数组中有一个对象的fc_start_time:09:30 am,该怎么办?您可以添加以下检查:ifa.fc_start_time.match/*pm/time1+=12;ifb.fc_start_time.match/*pm/time2+=12;就在他们回来之前。
Date.parse( '9/19/2014 ' + convertTo24Hour(time) );
function compare(a,b) {

  var atime = Date.parse('9/19/2014 ' + convertTo24Hour(a.time));
  var btime = Date.parse('9/19/2014 ' + convertTo24Hour(b.time));

  if (atime < btime) {
     return -1;
  }

  if (atime > btime) {
    return 1;
  }

  return 0;
}