Underscore.js | _.sortedIndex()

_.sortedIndex() function:

  • It decides where the new element you want to insert into the passed array will come in order the array remains in sorted order.
  • It is used in situations where you want to add a new element to the array but you do not know where to add it, i.e., at what position so as to make the array sorted.
  • It is not only restricted to the list of numbers but also to the other lists which contains characters etc.

Syntax:

_.sortedIndex(array, value, [iteratee], [context])

Parameters:
It takes three arguments:



  • The array
  • The value
  • The iteratee (optional)

Return value:
It returns the index where the new element should come in the array so that the array remains sorted.

Examples:

  1. Passing a list of numbers to _.sortedIndex() function:
    The ._sorted() function takes the element from the list one by one and checks whether the element is less than the new element or not. If it is less, then it is ignored and the _.sortedIndex() checks the same on the next element from the list. Otherwise if the element is greater then this function returns the index of this element. This means that the new element should come at this index and the elements from this index on wards first need to shift one step behind so as to make space for the new element.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.sortedIndex([1, 2, 4, 5, 6], 3));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  2. Passing another number list to the _.sortedIndex() function:
    We can pass any length of the array to the _.sortedIndex() function. It will perform a binary search to find the place of the new element. In binary search all the elements which are in the array are compared to the new element so as to find it’s new index. Finally, console.log() the new index found.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.sortedIndex([1, 2, 3, 4, 5, 6, 8], 7));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  3. Passing a structure to the _.sortedIndex() function:
    We can even pass a structure containing more than one key for property. In this we need to mention on the basis of which key we want to perform the binary search. Like in the below example we have 2 keys, which are name and the sal. And later we have passed the sal property as the comparison parameter after mentioning the element which we need to insert.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.sortedIndex([{name: 'amit', sal: 40000},
                                       {name: 'ankit', sal: 60000}, 
                                       {name: 'anju', sal: 80000}], 
                                       {name: 'akash', sal: 70000}, 
                                                           'sal'));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. Applying the search on a character:
    We can even perform the binary search on the characters rather than on the numbers. In this we pass a structure with 3 properties, name, rollNo and the section. In this we pass the third property for the comparison which contains the characters. The result will be in the similar way with no errors.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.sortedIndex([{name: 'akansha', rollNo: 01, section:'a'},
                                       {name: 'aishwarya', rollNo:02, section:'b'},
                                       {name: 'anjali', rollNo:03, section:'d'}],
                                       {name: 'preeti', rollNo:04, section:'c'}, 
                                                                      'section'));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

NOTE:
These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them.
The links are as follows:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- Write HTML code here -->
<script type="text/javascript" 
</script>

chevron_right


An example is shown below:



My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Shivam_k



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.