按小时对对象数组进行排序
Sort object array by hour
我有一个带有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;
}
我们最终得到了什么:
- 使用
array.sort
比较函数对元素进行排序 - 将时间转换为数字,以便我们能够正确地进行比较
- 为此,请将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;
});
试试这个。在你的时间里,你必须考虑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>
相关文章:
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 在数组的 2/3 上调用自身的排序算法
- 数组值的排序以匹配另一个数组ES6
- 按不同项目对对象数组进行排序
- 根据Javascript中的字符串值对数组的数组进行排序
- 在不同的javascript数组中对json响应进行排序
- 对对象数组中的数组进行排序
- 按特定键对对象数组进行排序
- 使用jquery根据数组对表行进行排序
- 尝试对对象数组进行排序但拼接不是一个函数
- 对javascript中某些键的对象数组进行排序
- Javascript按数字顺序排序()数组
- Javascript排序索引链接数组
- 在jquery中对时间数组进行排序
- 如何在具有相同 ID 的 Javascript 中按排序函数对关联数组进行排序
- Javascript中的多维数组排序索引问题
- 如何在mongodb中根据数组内容的差异进行排序
- 在Javascript中对具有特定异常的对象数组进行排序
- 数组数据排序类似mysql查询
- 在小于O(n)时间内找到(排序)数组中的重复元素