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.
arrays
New contributor
add a comment |
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.
arrays
New contributor
add a comment |
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.
arrays
New contributor
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
arrays
New contributor
New contributor
New contributor
asked Nov 8 at 9:47
A.J
32
32
New contributor
New contributor
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
A.J is a new contributor. Be nice, and check out our Code of Conduct.
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
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
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
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
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