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

Sometimes group_by_date results are not sorted #279

Closed
thebravoman opened this issue Mar 4, 2024 · 3 comments
Closed

Sometimes group_by_date results are not sorted #279

thebravoman opened this issue Mar 4, 2024 · 3 comments

Comments

@thebravoman
Copy link

This is the exact code


          render json: [
            { name: :assignments, data: assignments.group_by_day(:created_at).count },
            { name: :assignment_visits, data: assignment_visits.group_by_day(:created_at).count },
            { name: :messages, data: messages.group_by_day(:created_at).count },
            { name: :telegram_chats, data: telegram_chat_records.group_by_day(:created_at).count }
          ]

and this is the result

Screenshot 2024-03-04 at 19 52 51

Sometimes the results are not sorted by date and I don't understand why.
I think it is only here where the json is an array of data.

@ankane
Copy link
Owner

ankane commented Mar 5, 2024

Hi @thebravoman, JSON objects are unordered, so you'll need to convert them to arrays with to_a if you want to preserve the order.

@ankane ankane closed this as completed Mar 5, 2024
@thebravoman
Copy link
Author

thebravoman commented Mar 5, 2024

The getting started says another thing @ankane
https://github.com/ankane/groupdate?tab=readme-ov-file#getting-started

Results are returned in ascending order by default, so no need to sort.

Am I missing something?

The problem is not that the json is not sorted but that the

assignments.group_by_day(:created_at).count 

returns something that is not sorted and the getting started says there is no need to sort.

@ankane
Copy link
Owner

ankane commented Mar 5, 2024

If you can create a minimal reproducible example or test case that shows this, happy to look into it more.

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

2 participants