-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
_.sortedLastIndex #1882
Labels
Comments
Are you suggesting anything more complicated than below? _.sortedLastIndex = function(arr, item) {
var index = _.sortedIndex(arr, item);
for (; arr[index] !== item; index++) {}
return index;
}; |
If it work's for you all then that's fine (it doesn't return the expected result so would need tweaking). Usage example with expected results: var a = [1, 2, 2, 2, 3];
_.sortedIndex(a, 2); // expected `1`
_.sortedLastIndex(a, 2) // expected `4` |
My 2 cents in regards of @megawac code. if item is not present in the array then it will go in infinite loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Underscore has
_.sortedIndex
but this could be expanded to_.sortedLastIndex
using a shared helper between them both (minimal code increase). This would allow_.lastIndexOf
to support binary searches as well.The text was updated successfully, but these errors were encountered: