# Sorted Index

Find the insertion index in a sorted array using lodash _.sortedIndex

> Canonical page: https://elysiatools.com/en/tools/sorted-index

- **Category:** Data Processing

- **Keywords:** array, sorted, index, insert, lodash

## Overview

Return the index where a value should be inserted to keep an array sorted.

**Highlights:**
- Uses lodash `_.sortedIndex`
- Works with sorted JSON arrays
- Returns the insertion index as text

**Example:**
- Array: `[10, 20, 30]`
- Value: `25`
- Result: `2`

## Inputs

- **Array** (textarea): Enter a sorted JSON array
- **Value** (text): Value to insert (JSON or plain text)

## When to use

- When you need to insert a new element into a sorted array while preserving its order.
- For implementing efficient insertion operations in sorted datasets, such as in algorithms or data management.
- When working with sorted lists where manual index calculation is time-consuming or prone to errors.

## How it works

- Input a sorted JSON array in the 'Array' field, ensuring elements are in ascending order.
- Specify the value to insert in the 'Value' field, which can be a number, string, or other JSON-compatible type.
- The tool applies lodash's _.sortedIndex function to compute the insertion index using binary search.
- Receive the index as a text output, indicating the zero-based position where the value should be inserted to keep the array sorted.

## Use cases

- Inserting new scores into a sorted leaderboard in gaming or competition applications.
- Adding timestamps to a chronologically sorted log file for event tracking.
- Maintaining a sorted list of products by price or rating in e-commerce systems.

## Frequently asked questions

### What is a sorted array?

A sorted array has elements arranged in ascending order, like [10, 20, 30], where each element is greater than or equal to the previous one.

### How does _.sortedIndex determine the insertion index?

It uses binary search to find the lowest index where the value can be inserted to maintain the array's sort order, based on comparison.

### Can it handle arrays with duplicate values?

Yes, it returns the index of the first position where the value should be inserted, which may be before or after existing duplicates.

### What if my array isn't sorted?

The tool assumes the array is sorted; using an unsorted array may produce incorrect or unexpected index results.

### Is the returned index zero-based?

Yes, the index is zero-based, meaning the first position is 0, aligning with standard array indexing in programming.

## Related tools

- [Intersection Arrays](https://elysiatools.com/en/tools/intersection-arrays): Find common values across multiple arrays using lodash _.intersection
- [Find Key](https://elysiatools.com/en/tools/find-key): Find the first matching key using lodash _.findKey
- [Get Path](https://elysiatools.com/en/tools/get-path): Get a value at a path using lodash _.get
- [Group By](https://elysiatools.com/en/tools/group-by): Group items by a derived key using lodash _.groupBy
- [Head Item](https://elysiatools.com/en/tools/head-item): Get the first element of array using lodash _.head
- [Index Of](https://elysiatools.com/en/tools/index-of): Find the index of a value in array using lodash _.indexOf
- [Initial Items](https://elysiatools.com/en/tools/initial-items): Get all elements except the last using lodash _.initial
- [Nth Item](https://elysiatools.com/en/tools/nth-item): Get the item at a specific index using lodash _.nth

## Samples

- [QR Code Samples](https://elysiatools.com/en/samples/qrcode-samples): Sample QR code images for testing QR code reading and generation tools
- [Distributed Tracing Samples](https://elysiatools.com/en/samples/distributed-tracing-samples): Comprehensive distributed tracing examples using Jaeger, OpenTelemetry, and other modern observability tools for microservices architecture
- [ELK Stack Log Analysis Samples](https://elysiatools.com/en/samples/elk-stack-samples): Comprehensive ELK Stack (Elasticsearch, Logstash, Kibana) samples for log aggregation, processing, and visualization in distributed systems
- [Grafana Advanced Application Samples](https://elysiatools.com/en/samples/grafana-samples): Comprehensive Grafana samples covering advanced dashboard design, alerting configurations, data source integrations, and plugin development
