In this section, we will learn what the sort() method is and how to use it in JavaScript.
JavaScript URLSearchParams sort () Method
The `sort` method is used to sort the target query. The sort algorithm is based on the Unicode of the keys.
The method takes no argument and there’s no return value.
This method basically changes the original query.
URLSearchParams sort() Method Syntax:
searchParams.sort();
URLSearchParams sort () Method Parameter:
The method does not take a parameter.
URLSearchParams sort () Method Return Value:
There’s no return value from this method. (The return value is undefined)
Example: using URLSearchParams sort () method in JavaScript
const searchParams = new URLSearchParams("?name=John&lastName=Doe&address=Mars&name=Omid"); searchParams.sort(); console.log(searchParams);
Output:
address=Mars&lastName=Doe&name=John&name=Omid