php array sorting by Multiple Dates











up vote
0
down vote

favorite












Could an array be sorted by multiple dates, see example source below



Array
(
[0] => Array
(
[Name] => Test
[StartDate] => 2018-09-01 00:00:00
[EndDate] => 2018-09-07 00:00:00
[Days] => 7
)

[1] => Array
(
[Name] => Another Test
[StartDate] => 2018-11-05 00:00:00
[EndDate] => 2018-11-12 00:00:00
[Days] => 8
)
[2] => Array
(
[Name] => Something Else
[StartDate] => 2018-09-08 00:00:00
[EndDate] => 2018-10-01 00:00:00
[Days] => 24
)
[3] => Array
(
[Name] => Other
[StartDate] => 2018-11-13 00:00:00
[EndDate] => 2018-11-13 00:00:00
[Days] => 1
)
)


I've been trying to figure out how to re-arrange the array so that the array will be sorted by the start date, which I've managed to figure out perfectly.



What I'm struggling the most with is looping through the array again and re-arranging the array once more, so that the next event start date is > previous event end date, So what I would like to achieve is the order of 'Test', 'Something Else', 'Another Test', 'Other'



I've recently gotten to grips with arrays and learning quite quickly however I'm still a beginner, any help would be greatly appreciated.










share|improve this question







New contributor




A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    Could an array be sorted by multiple dates, see example source below



    Array
    (
    [0] => Array
    (
    [Name] => Test
    [StartDate] => 2018-09-01 00:00:00
    [EndDate] => 2018-09-07 00:00:00
    [Days] => 7
    )

    [1] => Array
    (
    [Name] => Another Test
    [StartDate] => 2018-11-05 00:00:00
    [EndDate] => 2018-11-12 00:00:00
    [Days] => 8
    )
    [2] => Array
    (
    [Name] => Something Else
    [StartDate] => 2018-09-08 00:00:00
    [EndDate] => 2018-10-01 00:00:00
    [Days] => 24
    )
    [3] => Array
    (
    [Name] => Other
    [StartDate] => 2018-11-13 00:00:00
    [EndDate] => 2018-11-13 00:00:00
    [Days] => 1
    )
    )


    I've been trying to figure out how to re-arrange the array so that the array will be sorted by the start date, which I've managed to figure out perfectly.



    What I'm struggling the most with is looping through the array again and re-arranging the array once more, so that the next event start date is > previous event end date, So what I would like to achieve is the order of 'Test', 'Something Else', 'Another Test', 'Other'



    I've recently gotten to grips with arrays and learning quite quickly however I'm still a beginner, any help would be greatly appreciated.










    share|improve this question







    New contributor




    A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Could an array be sorted by multiple dates, see example source below



      Array
      (
      [0] => Array
      (
      [Name] => Test
      [StartDate] => 2018-09-01 00:00:00
      [EndDate] => 2018-09-07 00:00:00
      [Days] => 7
      )

      [1] => Array
      (
      [Name] => Another Test
      [StartDate] => 2018-11-05 00:00:00
      [EndDate] => 2018-11-12 00:00:00
      [Days] => 8
      )
      [2] => Array
      (
      [Name] => Something Else
      [StartDate] => 2018-09-08 00:00:00
      [EndDate] => 2018-10-01 00:00:00
      [Days] => 24
      )
      [3] => Array
      (
      [Name] => Other
      [StartDate] => 2018-11-13 00:00:00
      [EndDate] => 2018-11-13 00:00:00
      [Days] => 1
      )
      )


      I've been trying to figure out how to re-arrange the array so that the array will be sorted by the start date, which I've managed to figure out perfectly.



      What I'm struggling the most with is looping through the array again and re-arranging the array once more, so that the next event start date is > previous event end date, So what I would like to achieve is the order of 'Test', 'Something Else', 'Another Test', 'Other'



      I've recently gotten to grips with arrays and learning quite quickly however I'm still a beginner, any help would be greatly appreciated.










      share|improve this question







      New contributor




      A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Could an array be sorted by multiple dates, see example source below



      Array
      (
      [0] => Array
      (
      [Name] => Test
      [StartDate] => 2018-09-01 00:00:00
      [EndDate] => 2018-09-07 00:00:00
      [Days] => 7
      )

      [1] => Array
      (
      [Name] => Another Test
      [StartDate] => 2018-11-05 00:00:00
      [EndDate] => 2018-11-12 00:00:00
      [Days] => 8
      )
      [2] => Array
      (
      [Name] => Something Else
      [StartDate] => 2018-09-08 00:00:00
      [EndDate] => 2018-10-01 00:00:00
      [Days] => 24
      )
      [3] => Array
      (
      [Name] => Other
      [StartDate] => 2018-11-13 00:00:00
      [EndDate] => 2018-11-13 00:00:00
      [Days] => 1
      )
      )


      I've been trying to figure out how to re-arrange the array so that the array will be sorted by the start date, which I've managed to figure out perfectly.



      What I'm struggling the most with is looping through the array again and re-arranging the array once more, so that the next event start date is > previous event end date, So what I would like to achieve is the order of 'Test', 'Something Else', 'Another Test', 'Other'



      I've recently gotten to grips with arrays and learning quite quickly however I'm still a beginner, any help would be greatly appreciated.







      arrays






      share|improve this question







      New contributor




      A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 8 at 9:47









      A.J

      32




      32




      New contributor




      A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      A.J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You should define a custom comparator. Something like:



          function cmp($a, $b)
          {
          if ($a.endDate== $b.startDate) {
          return 0;
          }
          return ($a.endDate < $b.startDate) ? -1 : 1;
          }

          usort($a, "cmp");


          EDIT



          To further explain what is happening. When you call a sorting function, whatever the underlying algorithm is it compares the elements to decide which one comes first. This order is trivially defined on primitive types (e.g. 1 is obviously less then 2), but on objects you may need to define how you want them to be ordered.



          In your case, you want that when you compare two objects, you want to apply some criteria based on the ending and starting date. That is what you do with a custom comparator.



          A comparator receives two elements to compare. It compares them, and if they are equal it returns 0. If the first one must be placed before the second one, it returns -1. If the first one must be placed after the second one, it returns +1. No other return values accepted.






          share|improve this answer























          • Thank you, I'll give that a try even though I don't fully understand it:)
            – A.J
            Nov 8 at 10:16










          • Edited the answer and added further details.
            – Tu.ma
            Nov 8 at 10:28






          • 1




            Thank you for the extended explanation, It helped me understand it better, I'll integrate it into my array for loop shortly.
            – A.J
            Nov 8 at 10:57










          • If you feel it is appropriate, please consider selecting the answer. Cheers
            – Tu.ma
            Nov 8 at 12:27











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


          }
          });






          A.J is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205135%2fphp-array-sorting-by-multiple-dates%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You should define a custom comparator. Something like:



          function cmp($a, $b)
          {
          if ($a.endDate== $b.startDate) {
          return 0;
          }
          return ($a.endDate < $b.startDate) ? -1 : 1;
          }

          usort($a, "cmp");


          EDIT



          To further explain what is happening. When you call a sorting function, whatever the underlying algorithm is it compares the elements to decide which one comes first. This order is trivially defined on primitive types (e.g. 1 is obviously less then 2), but on objects you may need to define how you want them to be ordered.



          In your case, you want that when you compare two objects, you want to apply some criteria based on the ending and starting date. That is what you do with a custom comparator.



          A comparator receives two elements to compare. It compares them, and if they are equal it returns 0. If the first one must be placed before the second one, it returns -1. If the first one must be placed after the second one, it returns +1. No other return values accepted.






          share|improve this answer























          • Thank you, I'll give that a try even though I don't fully understand it:)
            – A.J
            Nov 8 at 10:16










          • Edited the answer and added further details.
            – Tu.ma
            Nov 8 at 10:28






          • 1




            Thank you for the extended explanation, It helped me understand it better, I'll integrate it into my array for loop shortly.
            – A.J
            Nov 8 at 10:57










          • If you feel it is appropriate, please consider selecting the answer. Cheers
            – Tu.ma
            Nov 8 at 12:27















          up vote
          0
          down vote



          accepted










          You should define a custom comparator. Something like:



          function cmp($a, $b)
          {
          if ($a.endDate== $b.startDate) {
          return 0;
          }
          return ($a.endDate < $b.startDate) ? -1 : 1;
          }

          usort($a, "cmp");


          EDIT



          To further explain what is happening. When you call a sorting function, whatever the underlying algorithm is it compares the elements to decide which one comes first. This order is trivially defined on primitive types (e.g. 1 is obviously less then 2), but on objects you may need to define how you want them to be ordered.



          In your case, you want that when you compare two objects, you want to apply some criteria based on the ending and starting date. That is what you do with a custom comparator.



          A comparator receives two elements to compare. It compares them, and if they are equal it returns 0. If the first one must be placed before the second one, it returns -1. If the first one must be placed after the second one, it returns +1. No other return values accepted.






          share|improve this answer























          • Thank you, I'll give that a try even though I don't fully understand it:)
            – A.J
            Nov 8 at 10:16










          • Edited the answer and added further details.
            – Tu.ma
            Nov 8 at 10:28






          • 1




            Thank you for the extended explanation, It helped me understand it better, I'll integrate it into my array for loop shortly.
            – A.J
            Nov 8 at 10:57










          • If you feel it is appropriate, please consider selecting the answer. Cheers
            – Tu.ma
            Nov 8 at 12:27













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You should define a custom comparator. Something like:



          function cmp($a, $b)
          {
          if ($a.endDate== $b.startDate) {
          return 0;
          }
          return ($a.endDate < $b.startDate) ? -1 : 1;
          }

          usort($a, "cmp");


          EDIT



          To further explain what is happening. When you call a sorting function, whatever the underlying algorithm is it compares the elements to decide which one comes first. This order is trivially defined on primitive types (e.g. 1 is obviously less then 2), but on objects you may need to define how you want them to be ordered.



          In your case, you want that when you compare two objects, you want to apply some criteria based on the ending and starting date. That is what you do with a custom comparator.



          A comparator receives two elements to compare. It compares them, and if they are equal it returns 0. If the first one must be placed before the second one, it returns -1. If the first one must be placed after the second one, it returns +1. No other return values accepted.






          share|improve this answer














          You should define a custom comparator. Something like:



          function cmp($a, $b)
          {
          if ($a.endDate== $b.startDate) {
          return 0;
          }
          return ($a.endDate < $b.startDate) ? -1 : 1;
          }

          usort($a, "cmp");


          EDIT



          To further explain what is happening. When you call a sorting function, whatever the underlying algorithm is it compares the elements to decide which one comes first. This order is trivially defined on primitive types (e.g. 1 is obviously less then 2), but on objects you may need to define how you want them to be ordered.



          In your case, you want that when you compare two objects, you want to apply some criteria based on the ending and starting date. That is what you do with a custom comparator.



          A comparator receives two elements to compare. It compares them, and if they are equal it returns 0. If the first one must be placed before the second one, it returns -1. If the first one must be placed after the second one, it returns +1. No other return values accepted.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 10:27

























          answered Nov 8 at 10:02









          Tu.ma

          372111




          372111












          • Thank you, I'll give that a try even though I don't fully understand it:)
            – A.J
            Nov 8 at 10:16










          • Edited the answer and added further details.
            – Tu.ma
            Nov 8 at 10:28






          • 1




            Thank you for the extended explanation, It helped me understand it better, I'll integrate it into my array for loop shortly.
            – A.J
            Nov 8 at 10:57










          • If you feel it is appropriate, please consider selecting the answer. Cheers
            – Tu.ma
            Nov 8 at 12:27


















          • Thank you, I'll give that a try even though I don't fully understand it:)
            – A.J
            Nov 8 at 10:16










          • Edited the answer and added further details.
            – Tu.ma
            Nov 8 at 10:28






          • 1




            Thank you for the extended explanation, It helped me understand it better, I'll integrate it into my array for loop shortly.
            – A.J
            Nov 8 at 10:57










          • If you feel it is appropriate, please consider selecting the answer. Cheers
            – Tu.ma
            Nov 8 at 12:27
















          Thank you, I'll give that a try even though I don't fully understand it:)
          – A.J
          Nov 8 at 10:16




          Thank you, I'll give that a try even though I don't fully understand it:)
          – A.J
          Nov 8 at 10:16












          Edited the answer and added further details.
          – Tu.ma
          Nov 8 at 10:28




          Edited the answer and added further details.
          – Tu.ma
          Nov 8 at 10:28




          1




          1




          Thank you for the extended explanation, It helped me understand it better, I'll integrate it into my array for loop shortly.
          – A.J
          Nov 8 at 10:57




          Thank you for the extended explanation, It helped me understand it better, I'll integrate it into my array for loop shortly.
          – A.J
          Nov 8 at 10:57












          If you feel it is appropriate, please consider selecting the answer. Cheers
          – Tu.ma
          Nov 8 at 12:27




          If you feel it is appropriate, please consider selecting the answer. Cheers
          – Tu.ma
          Nov 8 at 12:27










          A.J is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          A.J is a new contributor. Be nice, and check out our Code of Conduct.













          A.J is a new contributor. Be nice, and check out our Code of Conduct.












          A.J is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205135%2fphp-array-sorting-by-multiple-dates%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff