how to sort JSON by date?











up vote
-2
down vote

favorite












this is my json:



enter image description here



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 ?










share|improve this question


















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

















up vote
-2
down vote

favorite












this is my json:



enter image description here



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 ?










share|improve this question


















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















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











this is my json:



enter image description here



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 ?










share|improve this question













this is my json:



enter image description here



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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 at 19:34









Mohandes

327




327








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
















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










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














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








share|improve this answer




























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





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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

























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








      share|improve this answer

























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








        share|improve this answer























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








          share|improve this answer












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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 at 19:42









          slider

          6,3051129




          6,3051129
























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





              share|improve this answer

























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





                share|improve this answer























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





                  share|improve this answer












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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 19:42









                  Jim B.

                  2,076828




                  2,076828






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin