admin管理员组文章数量:1323744
I have an array of objects with an hour(fc_start_time) property and I want to sort it by this property. I've tried this but it does not work for me. This is the object structure:
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"
}
Thanks in advance.
I have an array of objects with an hour(fc_start_time) property and I want to sort it by this property. I've tried this but it does not work for me. This is the object structure:
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"
}
Thanks in advance.
Share Improve this question edited May 23, 2017 at 11:43 CommunityBot 11 silver badge asked Sep 19, 2014 at 11:47 saRcasaRca 1422 silver badges10 bronze badges 2- 1 possible duplicate of Sorting objects in an array by a field value in JavaScript – apsillers Commented Sep 19, 2014 at 12:17
- apsillers, is the same concept, I didn't found it when research. However that solution doesn't work for my case. For example '11:00 am' and '11:00 pm' are very similar and both will be before than "12:00 pm" – saRca Commented Sep 19, 2014 at 13:56
4 Answers
Reset to default 3Dates and time, eh? I hate them. Let's try to figure this out.
How you sort an array? With help of array's sort method. By default this method sorts strings, but we need custom pare function here.
How can we pare time in strings? We can use Date
object, parse time, get milliseconds from 1970, and pare them. But we have am-pm
format here. So, to use Date.parse
method, we need to convert time to 24 hour format first (or use Date.js library).
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)/, '');
}
After we get 24h time, we can parse it like so (don't look at date before time, it doesn't matter, all we care about is time):
Date.parse( '9/19/2014 ' + convertTo24Hour(time) );
Now we can use this in array.sort
pare function. We just pare two numbers, 1411129800000 ? 1411132800000
, decide which one is bigger and sort array.
function pare(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;
}
What we got after all:
- Use
array.sort
pare function to sort elements - Convert time to number so we can correctly pare them
- To do so, convert 12h time format to 24 and use
Date.parse
Here is jsfiddle to play with - http://jsfiddle/rqgmkdbs/
Try this pare function according to the same time input as your example. It pares on the Floating notation of you time value.
<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 pare(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(pare);
console.log(objs);
</script>
var input = [{hour:1, minutes:10},{hour:4, minutes: 1}, ...];
input.sort(function (a, b)
{
// pare hours first
if (a.hour < b.hour) return -1;
if (a.hour > b.hour) return 1;
// else a.hour === b.hour, so pare 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;
});
Try this. You have to consider am
and pm
in your time.
<!doctype html>
</html>
<head>
<meta charset="utf-8">
<script src="http://ajax.googleapis./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>
本文标签: javascriptSort object array by hourStack Overflow
版权声明:本文标题:javascript - Sort object array by hour - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742117614a2421541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论