Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page collections sorted by date that include valid negative timestamps ordered wrong #1810

Closed
ghost opened this issue Dec 29, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 29, 2017

I have a number of pages I'm trying to sort by the 'date' field (descending), with some being dates before 1970 and thus being represented by a negative timestamp (though valid as the dates are between 1965 and 1970). Grav is sorting that collection like so:

  • 124141560
  • 97531320
  • 63664260
  • -148637760
  • -111733800
  • -31294320
  • -28268880

As you can see, the four negative timestamps are sorted in reverse. I've worked out that this is because Grav is sorting using the SORT_REGULAR flag, meaning it is first sorting the numbers as strings and then as a numbers. Changing lines 1204-1207 in Pages.php from:

case 'date':
    $list[$key] = $child->date();
    $sort_flags = SORT_REGULAR;
    break;

to:

case 'date':
    $list[$key] = $child->date();
    $sort_flags = SORT_NUMERIC;
    break;

leads to these dates to be ordered correctly.

I have tried using a call to order() with the 1 constant (as detailed by Flavio here #1199 (comment)), but the constant doesn't take effect because I'm sorting on date and thus the switch at line 1200 in Pages.php always sets the flag to SORT_REGULAR.

As date is represented by a number, shouldn't that line in the switch always be set to SORT_NUMERIC? If not, is there another fix?

I'm running Grav 1.3.10.

@rhukster
Copy link
Member

rhukster commented Jan 4, 2018

it actually sounds like it should be fixed in Grav itself. date should always be numeric, and therefore, should be sorted as such. I'll fix in 1.4 branch.

@rhukster rhukster added the bug label Jan 4, 2018
@rhukster
Copy link
Member

rhukster commented Jan 4, 2018

fixed here: 72abab6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants