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

Support numeric sort in ByParam #5305

Closed
smiileTeam opened this Issue Oct 11, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@smiileTeam
Copy link

smiileTeam commented Oct 11, 2018

func (p Pages) ByParam(paramsKey interface{}) Pages {

On this method, each value is cast to string so a compare beetwen (int) 9 and (int) 12 gives us 9 result first (instead of 12).
We would love to:

  • have an option to specify we want an order on ints
    or
  • have a new method ByIntParam which would do it
    or
  • maybe something that detect both v1 & v2 are ints and compare them without casting

@bep bep changed the title Order ByParam results in wrong order with int comparations Supprt numeric sort in ByParam Oct 11, 2018

@bep bep added the Enhancement label Oct 11, 2018

@bep

This comment has been minimized.

Copy link
Member

bep commented Oct 11, 2018

maybe something that detect both v1 & v2 are ints and compare them without casting

I suggest we do a variant of the above.

  • The different decoders (JSON, TOML, YAML) treats numbers differently, so an int may be int, int64 and I think even float64
  • If you in one page say myparam = 2 and in another say myparam = 2.2, I think most people expect them to be compared as numbers (2 comes before 2.2)

With that, I suggest that: If both are numbers, convert both to float64 and compare them.

@moorereason moorereason changed the title Supprt numeric sort in ByParam Support numeric sort in ByParam Oct 11, 2018

tryzniak added a commit to tryzniak/hugo that referenced this issue Jan 15, 2019

Support numeric sort in ByParam
With this commit ByParam takes into account a type of a value under a
key. If both values are numeric then they're coerced into float64 and
then get compared.
If any value isn't numeric, for example it's nil or string, then both
values coerced into string and get compared as strings
(lexicographicaly)

Nil values are always sent to the end.

Numeric values confirm to any type listed below:
uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64

Closes gohugoio#5305

tryzniak added a commit to tryzniak/hugo that referenced this issue Jan 15, 2019

Support numeric sort in ByParam
With this commit ByParam takes into account a type of a value under a
key. If both values are numeric then they're coerced into float64 and
then get compared.
If any value isn't numeric, for example it's nil or string, then both
values coerced into string and get compared as strings
(lexicographicaly)

Nil values are always sent to the end.

Numeric values confirm to any type listed below:
uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64

Closes gohugoio#5305

@bep bep closed this in #5606 Jan 20, 2019

bep added a commit that referenced this issue Jan 20, 2019

Support numeric sort in ByParam
With this commit ByParam takes into account a type of a value under a
key. If both values are numeric then they're coerced into float64 and
then get compared.
If any value isn't numeric, for example it's nil or string, then both
values coerced into string and get compared as strings
(lexicographicaly)

Nil values are always sent to the end.

Numeric values confirm to any type listed below:
uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64

Closes #5305
@smiileTeam

This comment has been minimized.

Copy link
Author

smiileTeam commented Jan 21, 2019

Nice, thanks

@bep bep modified the milestones: v0.53.1, v0.54 Feb 1, 2019

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