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

_.sortedIndex align with _.sortBy #1834

Closed
jdalton opened this issue Sep 3, 2014 · 5 comments · Fixed by #1939 or #2848
Closed

_.sortedIndex align with _.sortBy #1834

jdalton opened this issue Sep 3, 2014 · 5 comments · Fixed by #1939 or #2848
Labels

Comments

@jdalton
Copy link
Contributor

jdalton commented Sep 3, 2014

_.sortBy([undefined, 1, undefined, 2]);
// => [1, 2, undefined, undefined]
_.sortedIndex([1, 2, undefined, undefined], undefined);
// => 0; though should be 2
@megawac
Copy link
Collaborator

megawac commented Oct 22, 2014

Ping #1768

@arianf
Copy link
Contributor

arianf commented Feb 9, 2015

@megawac I believe I have a solution to this problem, that would work in all cases, but it changes the function around just a bit...

_.sortedIndex([1, 2, undefined, undefined], undefined);
// => 2

but the draw back to my solution is that for duplicate values in an array, it will give you the last index :

_.sortedIndex([10, 20, 30, 40, 50], 30);
// => 3

it basically gives you #1882 but to my defense, the definition of the function in the docs still holds true:

Uses a binary search to determine the index at which the value should be inserted into the list in order to maintain the list's sorted order. If an iteratee function is provided, it will be used to compute the sort ranking of each value, including the value you pass. The iteratee may also be the string name of the property to sort by (eg. length).

Which does insert it into the list in order to maintain the list's sorted order. What do you think?

@megawac
Copy link
Collaborator

megawac commented Feb 9, 2015

If you would like to share a gist @arianf I would be interested in seeing
it, even if it may not be suitable
On Feb 9, 2015 12:31 AM, "Arian" notifications@github.com wrote:

@megawac https://github.com/megawac I believe I have a solution to this
problem, that would work in all cases, but it changes the function around
just a bit...

_.sortedIndex([1, 2, undefined, undefined], undefined);// => 2

but the draw back to my solution is that for duplicate values in an array,
it will give you the last index :

_.sortedIndex([10, 20, 30, 40, 50], 30);// => 3

it basically gives you #1882
#1882 but to my defense,
the definition of the function in the docs still holds true:

Uses a binary search to determine the index at which the value should be
inserted into the list in order to maintain the list's sorted order. If an
iteratee function is provided, it will be used to compute the sort ranking
of each value, including the value you pass. The iteratee may also be the
string name of the property to sort by (eg. length).

Which does insert it into the list in order to maintain the list's
sorted order
. What do you think?


Reply to this email directly or view it on GitHub
#1834 (comment)
.

@arianf
Copy link
Contributor

arianf commented Feb 9, 2015

This is my old solution: https://gist.github.com/arianf/926c79e57d1779408cc9#file-orignal-solution-to-_-sortedindex

I've updated my solution to work the original way for duplicate values, I am about to make a pull request.

@amZotti
Copy link
Contributor

amZotti commented Feb 9, 2015

👍 Great work @arianf

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