Showing posts with label sortedindex. Show all posts
Showing posts with label sortedindex. Show all posts

Thursday, July 3, 2014

Lodash: Descending Sorted Index

The sortedIndex() function available in Lodash performs a binary search on an array to tell you where you can insert the new element in question, and maintain the sort order. Which implies, of course, that the array is already sorted. This function helps you keep it that way. The benefit to keeping and array sorted is that often, you're going to want to present the values of that array in sorted order. So you could, do it just-in-time, by sorting the array when it's about to be displayed. But this has efficiency problems.