how to sort JSON by date?
up vote
-2
down vote
favorite
this is my json
:
and i want to sort this json
by date and time. i write below code but it does not work.
this.newsArray.sort((a, b) =>
new Date(b.time).getTime() - new Date(a.time).getTime());
newsArray
contain 4 items as you can see in picture.
how can i fix this problem ?
javascript json angular typescript
add a comment |
up vote
-2
down vote
favorite
this is my json
:
and i want to sort this json
by date and time. i write below code but it does not work.
this.newsArray.sort((a, b) =>
new Date(b.time).getTime() - new Date(a.time).getTime());
newsArray
contain 4 items as you can see in picture.
how can i fix this problem ?
javascript json angular typescript
1
Your code does not reference thedate
property at all.
– TypeIA
Nov 17 at 19:36
sorting JSON?We usually sort Arrays that have an index
– El.
Nov 17 at 19:37
2
Please post your array as text, not as a screenshot.
– ggorlen
Nov 17 at 19:39
1
You need to compare the two date values correctly. See the constructor of Date and properly create a date object out ofitem.date
anditem.time
. Then, compare the two objects as described in this answer. There are also libraries for this case of date comparison (date-fns, moment.js, luxon, etc.)
– ssc-hrep3
Nov 17 at 19:40
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
this is my json
:
and i want to sort this json
by date and time. i write below code but it does not work.
this.newsArray.sort((a, b) =>
new Date(b.time).getTime() - new Date(a.time).getTime());
newsArray
contain 4 items as you can see in picture.
how can i fix this problem ?
javascript json angular typescript
this is my json
:
and i want to sort this json
by date and time. i write below code but it does not work.
this.newsArray.sort((a, b) =>
new Date(b.time).getTime() - new Date(a.time).getTime());
newsArray
contain 4 items as you can see in picture.
how can i fix this problem ?
javascript json angular typescript
javascript json angular typescript
asked Nov 17 at 19:34
Mohandes
327
327
1
Your code does not reference thedate
property at all.
– TypeIA
Nov 17 at 19:36
sorting JSON?We usually sort Arrays that have an index
– El.
Nov 17 at 19:37
2
Please post your array as text, not as a screenshot.
– ggorlen
Nov 17 at 19:39
1
You need to compare the two date values correctly. See the constructor of Date and properly create a date object out ofitem.date
anditem.time
. Then, compare the two objects as described in this answer. There are also libraries for this case of date comparison (date-fns, moment.js, luxon, etc.)
– ssc-hrep3
Nov 17 at 19:40
add a comment |
1
Your code does not reference thedate
property at all.
– TypeIA
Nov 17 at 19:36
sorting JSON?We usually sort Arrays that have an index
– El.
Nov 17 at 19:37
2
Please post your array as text, not as a screenshot.
– ggorlen
Nov 17 at 19:39
1
You need to compare the two date values correctly. See the constructor of Date and properly create a date object out ofitem.date
anditem.time
. Then, compare the two objects as described in this answer. There are also libraries for this case of date comparison (date-fns, moment.js, luxon, etc.)
– ssc-hrep3
Nov 17 at 19:40
1
1
Your code does not reference the
date
property at all.– TypeIA
Nov 17 at 19:36
Your code does not reference the
date
property at all.– TypeIA
Nov 17 at 19:36
sorting JSON?We usually sort Arrays that have an index
– El.
Nov 17 at 19:37
sorting JSON?We usually sort Arrays that have an index
– El.
Nov 17 at 19:37
2
2
Please post your array as text, not as a screenshot.
– ggorlen
Nov 17 at 19:39
Please post your array as text, not as a screenshot.
– ggorlen
Nov 17 at 19:39
1
1
You need to compare the two date values correctly. See the constructor of Date and properly create a date object out of
item.date
and item.time
. Then, compare the two objects as described in this answer. There are also libraries for this case of date comparison (date-fns, moment.js, luxon, etc.)– ssc-hrep3
Nov 17 at 19:40
You need to compare the two date values correctly. See the constructor of Date and properly create a date object out of
item.date
and item.time
. Then, compare the two objects as described in this answer. There are also libraries for this case of date comparison (date-fns, moment.js, luxon, etc.)– ssc-hrep3
Nov 17 at 19:40
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You're creating the Date object with just the time which results in an invalid date. Use the date and the time to create your date object:
const newsArray = [
{date: '2018-11-17', time: '18:35:00'},
{date: '2018-11-17', time: '17:35:00'},
{date: '2018-11-17', time: '16:20:00'},
{date: '2018-11-17', time: '20:39:00'},
];
const res = newsArray.sort((a, b) =>
new Date(b.date + ' ' + b.time).getTime() - new Date(a.date + ' ' + a.time).getTime());
console.log(res);
add a comment |
up vote
0
down vote
You're trying to construct a date from a string like '12:00:00' which is an invalid date.
Try combining .date and .time to make something Date can interpret:
this.newsArray.sort((a, b) =>
new Date(b.date + 'T' + b.time).getTime() - new Date(a.date + 'T' + a.time).getTime());
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You're creating the Date object with just the time which results in an invalid date. Use the date and the time to create your date object:
const newsArray = [
{date: '2018-11-17', time: '18:35:00'},
{date: '2018-11-17', time: '17:35:00'},
{date: '2018-11-17', time: '16:20:00'},
{date: '2018-11-17', time: '20:39:00'},
];
const res = newsArray.sort((a, b) =>
new Date(b.date + ' ' + b.time).getTime() - new Date(a.date + ' ' + a.time).getTime());
console.log(res);
add a comment |
up vote
3
down vote
accepted
You're creating the Date object with just the time which results in an invalid date. Use the date and the time to create your date object:
const newsArray = [
{date: '2018-11-17', time: '18:35:00'},
{date: '2018-11-17', time: '17:35:00'},
{date: '2018-11-17', time: '16:20:00'},
{date: '2018-11-17', time: '20:39:00'},
];
const res = newsArray.sort((a, b) =>
new Date(b.date + ' ' + b.time).getTime() - new Date(a.date + ' ' + a.time).getTime());
console.log(res);
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You're creating the Date object with just the time which results in an invalid date. Use the date and the time to create your date object:
const newsArray = [
{date: '2018-11-17', time: '18:35:00'},
{date: '2018-11-17', time: '17:35:00'},
{date: '2018-11-17', time: '16:20:00'},
{date: '2018-11-17', time: '20:39:00'},
];
const res = newsArray.sort((a, b) =>
new Date(b.date + ' ' + b.time).getTime() - new Date(a.date + ' ' + a.time).getTime());
console.log(res);
You're creating the Date object with just the time which results in an invalid date. Use the date and the time to create your date object:
const newsArray = [
{date: '2018-11-17', time: '18:35:00'},
{date: '2018-11-17', time: '17:35:00'},
{date: '2018-11-17', time: '16:20:00'},
{date: '2018-11-17', time: '20:39:00'},
];
const res = newsArray.sort((a, b) =>
new Date(b.date + ' ' + b.time).getTime() - new Date(a.date + ' ' + a.time).getTime());
console.log(res);
const newsArray = [
{date: '2018-11-17', time: '18:35:00'},
{date: '2018-11-17', time: '17:35:00'},
{date: '2018-11-17', time: '16:20:00'},
{date: '2018-11-17', time: '20:39:00'},
];
const res = newsArray.sort((a, b) =>
new Date(b.date + ' ' + b.time).getTime() - new Date(a.date + ' ' + a.time).getTime());
console.log(res);
const newsArray = [
{date: '2018-11-17', time: '18:35:00'},
{date: '2018-11-17', time: '17:35:00'},
{date: '2018-11-17', time: '16:20:00'},
{date: '2018-11-17', time: '20:39:00'},
];
const res = newsArray.sort((a, b) =>
new Date(b.date + ' ' + b.time).getTime() - new Date(a.date + ' ' + a.time).getTime());
console.log(res);
answered Nov 17 at 19:42
slider
6,3051129
6,3051129
add a comment |
add a comment |
up vote
0
down vote
You're trying to construct a date from a string like '12:00:00' which is an invalid date.
Try combining .date and .time to make something Date can interpret:
this.newsArray.sort((a, b) =>
new Date(b.date + 'T' + b.time).getTime() - new Date(a.date + 'T' + a.time).getTime());
add a comment |
up vote
0
down vote
You're trying to construct a date from a string like '12:00:00' which is an invalid date.
Try combining .date and .time to make something Date can interpret:
this.newsArray.sort((a, b) =>
new Date(b.date + 'T' + b.time).getTime() - new Date(a.date + 'T' + a.time).getTime());
add a comment |
up vote
0
down vote
up vote
0
down vote
You're trying to construct a date from a string like '12:00:00' which is an invalid date.
Try combining .date and .time to make something Date can interpret:
this.newsArray.sort((a, b) =>
new Date(b.date + 'T' + b.time).getTime() - new Date(a.date + 'T' + a.time).getTime());
You're trying to construct a date from a string like '12:00:00' which is an invalid date.
Try combining .date and .time to make something Date can interpret:
this.newsArray.sort((a, b) =>
new Date(b.date + 'T' + b.time).getTime() - new Date(a.date + 'T' + a.time).getTime());
answered Nov 17 at 19:42
Jim B.
2,076828
2,076828
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354815%2fhow-to-sort-json-by-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Your code does not reference the
date
property at all.– TypeIA
Nov 17 at 19:36
sorting JSON?We usually sort Arrays that have an index
– El.
Nov 17 at 19:37
2
Please post your array as text, not as a screenshot.
– ggorlen
Nov 17 at 19:39
1
You need to compare the two date values correctly. See the constructor of Date and properly create a date object out of
item.date
anditem.time
. Then, compare the two objects as described in this answer. There are also libraries for this case of date comparison (date-fns, moment.js, luxon, etc.)– ssc-hrep3
Nov 17 at 19:40