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

Sort::by, Sort::byDescending and Sort::user have the same results (DESC) #11

Open
KarelWintersky opened this issue Nov 1, 2023 · 0 comments

Comments

@KarelWintersky
Copy link

Source dataset

array (
  0 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-31',
  ),
  1 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-30',
  ),
  2 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-29',
  ),
)

After "by Ascending"

Sort::by($dataset, static function($element) {
    return $element['event_date'];
});
array (
  0 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-31',
  ),
  1 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-30',
  ),
  2 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-29',
  ),
)

After "by Descending":

Sort::byDescending($dataset, static function($element) {
    return $element['event_date'];
});

same result

array (
  0 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-31',
  ),
  1 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-30',
  ),
  2 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-29',
  ),
)

After user func:

Sort::user($dataset, static function($first, $second) {
    return $second['event_date'] <=> $first['event_date'];
    // return $first['event_date'] <=> $second['event_date']; // doesn't matter, same result
});

same result

BUT:

usort($dataset, static function($first, $second) {
    return $first['event_date'] <=> $second['event_date'];
});

return:

array (
  0 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-29',
  ),
  1 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-30',
  ),
  2 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-31',
  ),
)

as expected

usort($dataset, static function($first, $second) {
    return $second['event_date'] <=> $first['event_date'];
});

returns

array (
  0 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-31',
  ),
  1 => 
  array (
    'item_id' => '240113',    'event_date' => '2023-10-30',
  ),
  2 =>   array (
    'item_id' => '240113',    'event_date' => '2023-10-29',
  ),
)

As expected too

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

No branches or pull requests

1 participant