Skip to content
Related Articles

Related Articles

Node.js URLSearchParams.sort()

View Discussion
Improve Article
Save Article
  • Last Updated : 14 Oct, 2021
View Discussion
Improve Article
Save Article

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

Syntax:

searchParams.sort();

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

Example1:




// Create a test URLSearchParams object 
var 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

Example2:




// Create a test URLSearchParams object 
var 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
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!