Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.sortedindex() Function

Underscore.js _.sortedindex() Function

_.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 iterate (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.
     

html




<!-- 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>


  1. 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.
     

html




<!-- 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>


  1. Output:
  2. 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.
     

html




<!-- 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>


  1. Output:
  2. 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.
     

html




<!-- 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>


  1. 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:
 

html




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


An example is shown below:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments