Underscore.js _.sortedindex() Function Last Updated : 29 Oct, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report _.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 arrayThe valueThe iterate (optional) Return value:It returns the index where the new element should come in the array so that the array remains sorted. Examples: 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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script type="text/javascript"> console.log(_.sortedIndex([1, 2, 4, 5, 6], 3)); </script> </body> </html> Output: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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script type="text/javascript"> console.log(_.sortedIndex([1, 2, 3, 4, 5, 6, 8], 7)); </script> </body> </html> Output: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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </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> Output: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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </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> 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" src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> An example is shown below: Comment More infoAdvertise with us S Sakshi98 Follow Improve Article Tags : JavaScript Web Technologies JavaScript - Underscore.js Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read Like