Open In App

Node.js URLSearchParams.sort()

Improve
Improve
Like Article
Like
Save
Share
Report

In the URLSearchParams interface, the sort() method helps to sort all keys/pairs in place. The sort criteria are unicode points of the keys. This method uses a stable sorting algorithm. 

Syntax:

searchParams.sort();

Return: Sorted order of existing name-value pairs in place by their names. 

Example 1: In this example, we will see the use of URLSeacrchParams().

javascript




// Create a test URLSearchParams object
const searchPars = new URLSearchParams("d=4 & c=2 & b=3 & a=1");
 
// Sort the key/value pairs
searchPars.sort();
 
// Display the sorted query string
console.log(searchPars.toString());


Output:

a=1&b=3&c=2&d=4

Example 2: In this example, we will see the use of URLSearchParams().

javascript




// Create a test URLSearchParams object
const searchPars = new URLSearchParams(z=4 & a=2 & t=3 & a=1");
 
// Sort the key/value pairs
searchPars.sort();
 
// Display the sorted query string
console.log(searchPars.toString());


Output:

a=2&a=1&t=3&z=4

Supported Browsers:

  • Google Chrome
  • IE
  • Edge
  • Opera
  • Apple Safari

Last Updated : 31 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads