按小时对对象数组进行排序

Sort object array by hour

本文关键字:排序 数组 对象 小时      更新时间:2023-09-26

我有一个带有hour(fc_start_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"
}

提前谢谢。

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

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

我们如何用字符串来比较时间?我们可以使用Date对象,解析时间,获取1970年的毫秒数,并对它们进行比较。但是我们这里有am-pm格式。因此,要使用Date.parse方法,我们需要首先将时间转换为24小时格式(或使用Date.js库)。

function convertTo24Hour(time) {
  var hours = parseInt(time.substr(0, 2));
  if(time.indexOf('am') != -1 && hours == 12) {
    time = time.replace('12', '0');
  }
  if(time.indexOf('pm')  != -1 && hours < 12) {
    time = time.replace(hours, (hours + 12));
  }
  return time.replace(/(am|pm)/, '');
}

当我们得到24小时的时间后,我们可以这样解析它(不要在时间之前看日期,这无关紧要,我们关心的只是时间):

Date.parse( '9/19/2014 ' + convertTo24Hour(time) );

现在我们可以在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;
}

我们最终得到了什么:

  1. 使用array.sort比较函数对元素进行排序
  2. 将时间转换为数字,以便我们能够正确地进行比较
  3. 为此,请将12小时时间格式转换为24小时,然后使用Date.parse

这里有jsfiddle可以玩-http://jsfiddle.net/rqgmkdbs/

根据与示例相同的时间输入尝试此比较函数。它比较您的时间值的浮点表示法。

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

试试这个。在你的时间里,你必须考虑ampm

<!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>