Reputation: 939
Is it possible to rearrange the below JSON format based on time (2016-12-07T13:00:00) using jQuery/Javascript.
[
{
"id":1,
"start":"2016-12-07T13:00:00",
"subject":"test1",
},
{
"id":2,
"start":"2016-12-07T09:00:00",
"subject":"test2",
},
{
"id":3,
"start":"2016-12-07T10:00:00",
"subject":"test3",
},
{
"id":4,
"start":"2016-12-07T07:00:00",
"subject":"test4",
},
{
"id":5,
"start":"2016-12-07T14:00:00",
"subject":"test5",
}
]
Upvotes: 1
Views: 6732
Reputation: 746
//put to variable
var db = [
{
"id":1,
"start":"2016-12-07T13:00:00",
"subject":"test1",
},
{
"id":2,
"start":"2016-12-07T09:00:00",
"subject":"test2",
},
{
"id":3,
"start":"2016-12-07T10:00:00",
"subject":"test3",
},
{
"id":4,
"start":"2016-12-07T07:00:00",
"subject":"test4",
},
{
"id":5,
"start":"2016-12-07T14:00:00",
"subject":"test5",
}
];
//use .sort()
db.sort(
function(a,b){
//use new Date parse string to date type
//convert date to number use Date.parse()
//format function(a,b) { return a-b; }
return Date.parse(new Date(a.start)) - Date.parse(new Date(b.start));
}
);
console.log(db)
Upvotes: 1
Reputation: 386654
You could use String#localeCompare
in a sort callback for the property start
, because ISO 8601 dates are sortable as string.
var array = [
{ id: 1, start: "2016-12-07T13:00:00", subject: "test1" },
{ id: 2, start: "2016-12-07T09:00:00", subject: "test2" },
{ id: 3, start: "2016-12-07T10:00:00", subject: "test3" },
{ id: 4, start: "2016-12-07T07:00:00", subject: "test4" },
{ id: 5, start: "2016-12-07T14:00:00", subject: "test5" }
];
array.sort(function (a, b) {
return a.start.localeCompare(b.start);
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 6
Reputation: 1462
Try this maybe helpful,
var aryData =[
{
"id":1,
"start":"2016-12-07T13:00:00",
"subject":"test1",
},
{
"id":2,
"start":"2016-12-07T09:00:00",
"subject":"test2",
},
{
"id":3,
"start":"2016-12-07T10:00:00",
"subject":"test3",
},
{
"id":4,
"start":"2016-12-07T07:00:00",
"subject":"test4",
},
{
"id":5,
"start":"2016-12-07T14:00:00",
"subject":"test5",
}
];
function comp(a, b) {
return new Date(a.start).getTime() - new Date(b.start).getTime();
}
aryData.sort(comp);
console.log(aryData);
Upvotes: 0
Reputation: 5760
Yes, you would use the javascript array sort method ( http://www.w3schools.com/jsref/jsref_sort.asp ).
Define your array as assign it to an variable then call the sort method, you can pass the sort method a function to perform the detail of the comparison.
var aryData = [{"id":1
,"start":"2016-12-07T13:00:00"
,"subject":"test1"
},{
"id":2
,"start":"2016-12-07T09:00:00"
,"subject":"test2"
},{
"id":3
,"start":"2016-12-07T10:00:00"
,"subject":"test3"
},{
"id":4
,"start":"2016-12-07T07:00:00"
,"subject":"test4"
},{
"id":5
,"start":"2016-12-07T14:00:00"
,"subject":"test5"
}];
aryData.sort(function(a, b) {
var dtA = new Date(a['start'])
,dtB = new Date(b['start']);
return ( dtA.getTime() - dtB.getTime() );
});
Upvotes: 0
Reputation: 819
If You can use External Library For example Lodash
Assign the json string to variable and try
var sort = _.sortBy(variable_name, "start")
hope this Helps you.
Upvotes: 0