sortBy() seems to sort negative numbers incorrectly

Hi there,

in my page I have a field with dates as milliseconds since epoch. As some of these dates took time before 1970-01-01, they have negative milliseconds values. When I try to sort a collection by this field, I get incorrect results.

$sorted = $my_dates->sortBy('ms');

// results in something like
// -348667200000
// -452260800000
// -1084968000000
// -1283601600000
// -1286020800000
// 337777200000
// 459774000000
// 1402657200000

Looks like the negative numbers are sorted by increasing absolute value, which is incorrect. Is this a bug? What can I do to work around this?


sortBy() accepts a third parameter sortBy($field, $directed, $flag), sort flag,

SORT_NUMERIC would be correct in this case.

Thanks @texnixe.
And while I now realize this is mentioned in the sort recipe, it is kind of hidden and is completely missing in the reference (which is where I look for such infos). Could this be included in the reference of the sortBy() method? Thanks!

Those parameters should actually pop up automatically, will try to figure out why they don’t.

1 Like