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

fix: keep orignal order if inputs are nil for caseInsensitiveCmp #191

Closed

Conversation

richardo2016
Copy link

in function caseInsensitiveCmp, to keep original order when other items with specified property undefined, we should use logic like below:

function caseInsensitiveCmp (a: any, b: any) {
    if (!a && !b) return 1
    else if (!a) return 1
    else if (!b) return -1
    ...
}

simplify it, we got

function caseInsensitiveCmp (a: any, b: any) {
    if (!a) return 1
    if (!b) return -1
    ...
}

@harttle
Copy link
Owner

harttle commented Feb 20, 2020

Your newly added case failed, see travis log. I tried shopify/liquid but find it rather confusing:

// scope in ruby
scope = { "students" => [
    { "name" => 'bob' },
    { "name" => 'alice', "age" => 2 },
    { "name" => 'amber' }
]}

// template
{{ students | sort_natural: "age" | map: "name" | join }}

// output
alice amber bob

It seems that those items without the specified property are moved to the tail, and they are re-ordered alphabetically. I'm not sure whether keys in Ruby Map are ordered or not, but certainly it's not behaving the same way as JavaScript.

@harttle harttle closed this in f57156b Mar 4, 2020
harttle pushed a commit that referenced this pull request Mar 4, 2020
# [9.11.0](v9.10.0...v9.11.0) (2020-03-04)

### Bug Fixes

* `Buffer not defined` for browser bundles, fixes [#197](#197) ([65b849c](65b849c))
* stable sort for undefined keys, fixes [#191](#191) ([f57156b](f57156b))

### Features

* async cache.read()/write(), remove .has() ([61dac49](61dac49))
@harttle
Copy link
Owner

harttle commented Mar 4, 2020

🎉 This issue has been resolved in version 9.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@harttle
Copy link
Owner

harttle commented Mar 4, 2020

Finally I find out that in Ruby all items with a null property value goes to the tail of the sorted list. I altered the test case a bit and return 0 for a == null && b == null to keep the original order. There're still differences in sorting and property iteration order, I added some notes in README.md.

Though this PR can't be merged due to the test case not passing, thank you for pointing this out and initiating the test case, @all-contributors please add richardo2016 for code

@allcontributors
Copy link
Contributor

@harttle

I've put up a pull request to add @richardo2016! 🎉

@richardo2016
Copy link
Author

Finally I find out that in Ruby all items with a null property value goes to the tail of the sorted list. I altered the test case a bit and return 0 for a == null && b == null to keep the original order. There're still differences in sorting and property iteration order, I added some notes in README.md.

Though this PR can't be merged due to the test case not passing, thank you for pointing this out and initiating the test case, @all-contributors please add richardo2016 for code

thx for your attention to this feature!

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

Successfully merging this pull request may close these issues.

2 participants