How to get index in a sorted array based on iterator function in JavaScript?


Iterator functions are used to iterate through arrays, lists, and other data structures. In JavaScript, there are several ways to get the index at which a value should be inserted into a sorted array.

Using the Array sort() and indexOf() Methods

The sort() method can be used to sort an array in ascending or descending order. The default sort order is ascending, so if you want to get the index at which a value should be inserted into a sorted array, you can use the sort() method with a compare function. After the array is sorted we could apply the indexOf() method to find the index of the value to be inserted in the array.

Syntax

function compare(a, b) {
   return a - b;
}
arr.sort(compare);
var index = arr.indexOf(num);

The compare function “compare” takes two arguments, a and b, and returns a positive number if a is greater than b, a negative number if a is less than b, or 0 if a is equal to b. The arr is the original array to that we want to insert the value num.

Algorithm

  • STEP 1 − Define the compare function.

  • STEP 2 − Create an array named arr and assign the values to it.

  • STEP 3 − Create a variable named num. The index of this element, we need to find in the array arr.

  • STEP 4 − Sort the array based on the compare function using arr.sort() method.

  • STEP 5 − Find the index at which the num should be inserted in the sorted array using arr.indexOf(num).

  • STEP 6 − Display the index on the window screen using innerHTML property.

Example

For example, if you have an array of numbers and you want to get the index at which a given number should be inserted into the array in ascending order, you can use the following compare program.

<!doctype html> <html> <head> <title>Examples</title> </head> <body> <h3>Using the Array sort() and indexOf() Methods</h3> <p>The index in the sorted array</p> <div id="result"></div> <script> function compare(a, b) { return a - b; } var arr = [1, 2, 3, 4, 5]; var num = 3; arr.sort(compare); var index = arr.indexOf(num); document.getElementById("result").innerHTML = index </script> </body> </html>

Using the findIndex() Method

The findIndex() method can be used to find the index of the first element in an array that satisfies a given condition. For example, if you have an array of numbers and you want to get the index at which a given number should be inserted into the array in ascending order, you can use the following compare function −

Example

In the below example, we the index of the value num using findIndex() method.

<!doctype html> <html> <head> <title>Examples</title> </head> <body> <h3>Using the findIndex() Method</h3> <p>The index to that the value to be inserted</p> <div id="result"></div> <script> function compare(a, b) { return a - b; } var arr = [1, 2, 3, 4, 5]; var num = 3; var index = arr.findIndex(compare); document.getElementById("result").innerHTML = index </script> </body> </html>

There are several ways to get the index at which a value should be inserted into a sorted array in JavaScript. You can use the sort() method with a compare function, or you can use the binarySearch() method with a compare function.

Updated on: 04-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements