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 broken for date #1412

Closed
julianbarg opened this issue Nov 3, 2022 · 7 comments
Closed

Sort broken for date #1412

julianbarg opened this issue Nov 3, 2022 · 7 comments
Labels

Comments

@julianbarg
Copy link

julianbarg commented Nov 3, 2022

Describe the bug
I am trying to sort by a dictionary by a value that contains numbers. The sorting does not work, regardless of whether I use the value as-is or convert it to string.

Version of yq: 4.29.2
Operating system: linux
Installed via: snap

Input

- key: a
  date: 2011-09-12
- key: c
  date: 2011-10-05
- key: b
  date: 2010-12-09
- key: d
  date: 2011-10-20
- key: e
  date: 2010-09-29

I tried the following:

Command 1

yq '. | sort_by(.date)' ~/input.yaml

Output 1

- key: e
  date: 2010-09-29
- key: d
  date: 2011-10-20
- key: b
  date: 2010-12-09
- key: c
  date: 2011-10-05
- key: a
  date: 2011-09-12

Command 2

yq '[.[] | .date style="double"] | sort_by(.date)' input.yaml

Output 2

- key: e
  date: "2010-09-29"
- key: d
  date: "2011-10-20"
- key: b
  date: "2010-12-09"
- key: c
  date: "2011-10-05"
- key: a
  date: "2011-09-12"

Command 3

yq '[.[] | .date style="double"] |= sort_by(.date)' input.yaml
- key: a
  date: "2011-09-12"
- key: c
  date: "2011-10-05"
- key: b
  date: "2010-12-09"
- key: d
  date: "2011-10-20"
- key: e
  date: "2010-09-29"

Expected behavior

- key: e
  date: "2010-09-29"
- key: b
  date: "2010-12-09"
- key: a
  date: "2011-09-12"
- key: c
  date: "2011-10-05"
- key: d
  date: "2011-10-20"

Additional context
Looks like for some reason yq is reversing the order of the list. Except for the third command that is. I have no idea what is going on, but certainly is not expected behavior.

@mikefarah mikefarah changed the title Sort broken for numbers Sort broken for date Nov 3, 2022
@mikefarah
Copy link
Owner

Yep that's a bug - I haven't added support for dates/timestamps in sort - will fix in next release!

@julianbarg
Copy link
Author

Love to hear it. Note that the issue also occurs when converting the date to a string before sorting. That is a reliable workaround when using yymmdd because you effectively sort by number. Any idea why that is?

@mikefarah
Copy link
Owner

Yep, that transforms it to an integer, which will sort correctly 👍🏼

@julianbarg
Copy link
Author

If you look at my command 2/3, when we just have integers in a string, it does not sort correctly also.

mikefarah added a commit that referenced this issue Nov 4, 2022
@mikefarah
Copy link
Owner

I can't replicate that :/

./yq '[.[] | .date style="double"] | sort_by(.date)' examples/data1.yaml 
- key: e
  date: "2010-09-29"
- key: b
  date: "2010-12-09"
- key: a
  date: "2011-09-12"
- key: c
  date: "2011-10-05"
- key: d
  date: "2011-10-20"

Note that the style parameter does not affect the value for sorting:

./yq 'sort_by(.date)' examples/data1.yaml  
- key: e
  date: "2010-09-29"
- key: b
  date: "2010-12-09"
- key: a
  date: "2011-09-12"
- key: c
  date: "2011-10-05"
- key: d
  date: "2011-10-20"

@julianbarg
Copy link
Author

Good to know. May circle back on this if troubleshooting yields something relevant.

@julianbarg
Copy link
Author

Just coming back to this to let you know that v4.30.4 has fixed the problem for me.

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

No branches or pull requests

2 participants