JavaScript TypedArray sort() Method

In JavaScript, the method TypedArray.sort is a built-in function associated with TypedArray.prototype. This method is used to arrange the elements within a typed array in a specific order and subsequently returns a reference to the original typed array. TypedArrays are binary arrays with a fixed length that exclusively hold numeric data types, including integers, floats, or bigints.

Syntax:

Example

typedArray.sort(compareFunction)

Parameters:

compareFunction: This parameter is not mandatory. It is a function that specifies the order in which items should be sorted. If this function is not provided, the elements will be arranged numerically in ascending order by default.

Return value:

The result produced by typedArray consists of the elements arranged in a sorted order, retaining the same reference to the original array.

Uses of TypedArray sort in JavaScript

Below is an example demonstrating the use of the sort method available on TypedArrays in JavaScript:

  • Sorting Numeric Values: In JavaScript, the prototype.sort function is instrumental for arranging elements in numerical order. This functionality is particularly beneficial for typed arrays, which are optimized for managing numerical binary data with high efficiency.
  • Arranging Numeric Values: In JavaScript, typed arrays are frequently employed for the storage of numerical information. For instance, arrays like Uint8Array, Int32Array, and Float64Array are commonly used. When the sort method is executed on these typed arrays, it directly organizes these numerical entries.
  • Example 1: JavaScript TypedArray sort Method

In the following illustration, we will present a JavaScript example to gain insight into the sort method of TypedArray.

Example

Example

// Create a typed array (Uint8Array holds unsigned 8-bit integers)

const typedArray = new Uint8Array([42, 7, 99, 3, 15]);

// Sort the typed array

typedArray.sort();  // It always sorts numerical values in ascending order

// Print the sorted typed array

console.log(typedArray);

Output:

Output

Uint8Array [ 3, 7, 15, 42, 99 ]

Explanation:

In the preceding example, we established a variable called typedArray utilizing const. This typed array is designed to contain 8-bit integers. Subsequently, we employed the typedArray.sort method on this array, which arranges the elements in numerical order from lowest to highest and outputs the result to the console.

Example 2: Sorting in descending Order

In the following example, we will explore the process of arranging a typed array in descending order utilizing the sort method.

Example

Example

const typedArray = new Int16Array([30,10, 5, 55, 12, 9]);

typedArray.sort(); 

typedArray.reverse();

console.log(typedArray);

Output:

Output

Int16Array [ 55, 30, 12, 10, 9, 5 ]

Explanation:

In the preceding example, we established a variable referred to as typedArray utilizing the const keyword. In this instance, we employed the typedArray.sort method, which initially arranges the elements of the array in ascending order. Subsequently, we invoked the typedArray.reverse method, which reverses the sequence of the elements, thereby sorting the array in descending order.

Example 3: Sorting a Float32Array with negative and positive values

In the following example, we will demonstrate how to arrange a Float32Array, which is a type of TypedArray containing both negative and positive numbers, by employing the sort method.

Example

Example

const float32TypedArray = new Float32Array ([-2.4, 1.3, -0.6, 0.0]);

float32TypedArray.sort();

console.log(float32TypedArray);

Output:

Output

Float32Array [

  -2.4000000953674316,

  -0.6000000238418579,

  0,

  1.2999999523162842

]

Explanation:

In the preceding example, we established a variable referred to as float32TypedArray. This variable is assigned an array that includes both negative and positive numbers. We utilized the float32TypedArray.sort method to arrange the elements of the array in numerical order, and subsequently, we displayed the output using console.log.

Example 4: Sorting a BigInt64Array

In this instance, we will arrange a BigInt64Array in JavaScript by employing the sort function.

Example

Example

const bigInt64Array = new BigInt64Array([5n, 2n, 10n, 8n]);

bigInt64Array.sort();

console.log(bigInt64Array)

Output:

Output

BigInt64Array(4) [ 2n, 5n, 8n, 10n ]

Explanation:

In the preceding example, we declared a variable called bigInt64Array with the const keyword. We initialized it with an array. We utilized the bigInt64Array.sort function to arrange the elements of the array in ascending order and subsequently displayed the outcome using console.log.

Browser Support:

Chrome Yes
Safari Yes
Firefox Yes
Opera Yes
Edge Yes

Conclusion:

In JavaScript, the sort method associated with TypedArrays is an incredibly beneficial tool. It alters the existing array in place instead of generating a new array. This functionality proves to be highly effective when dealing with extensive datasets. In this article, we have explored the typedArray sort method through a variety of examples.

FAQs:

  1. What is the primary function of TypedArray.prototype.sort in JavaScript?

The primary function of the TypedArray prototype.sort method in JavaScript is to arrange the elements of the typed array in ascending order while returning the original array reference. This method organizes the elements of the array in a numerical ascending sequence.

  1. Does sort generate a new typed array?

No, it does not generate a new typed array. Instead, it merely organizes the elements of the existing typed array and provides a reference to that same array. If our goal is to create a copy without altering the original array, we should utilize the TypedArray.prototype.tosorted method.

  1. How does TypedArray sort function internally in JavaScript?

The sort method for TypedArray is designed to organize the elements of the array directly within the original array. By default, this method arranges the elements in ascending order. The comparisons made between the elements are conducted numerically.

  1. When comparing the TypedArray.sort method to Array.sort in JavaScript, what distinctions can be identified?

In JavaScript, the fundamental distinction between the TypedArray.sort method and the Array.sort method lies in their inherent sorting behavior. The TypedArray.sort method arranges the numerical values within the array in ascending order. Conversely, the Array.sort method organizes the array elements based on their string representations.

  1. Is the TypedArray.prototype.sort method widely supported?

The TypedArray.prototype.sort function is a prominent method that enjoys broad support and utilization across contemporary web browsers in JavaScript.

Input Required

This code uses input(). Please provide values below: