Node.js lodash.sortBy() Function14 Feb 2025 | 4 min read In this article, you will learn about the Node.js lodash.sortBy() function with its syntax, parameters, and examples. What is Node.js lodash.sortBy() function?Underscore.js may be extended with the use of Lodash, a well-liked Node.js utility library. The range of functions it provides makes working with arrays, strings, objects, numbers, and other data types easier. The method _.sortBy() is one of its useful methods. It sorts an array according to predetermined criteria in ascending order. With the ability to provide one or more iterates to sort the elements, this method can handle complicated sorting requirements. Syntax:It has the following syntax: Parameters:
Return Value:The sorted collection is returned. Example 1:lLet us take an example to illustrate the Node.js lodash.sortBy() function. Output: Before sorting, the elements are: [ 3, 2, 7, 6, 4, 8 ] After sorting, the elements are: [ 2, 3, 4, 6, 7, 8 ] Explanation:In this example, the Lodash library is imported using the variable "l" in this bit of Node.js code, which also generates an array with numeric elements [3, 2, 7, 6, 4, 8]. After that, the console.log() is used to log the array to the console to display the unsorted array in its initial state. Next, the elements of the array are sorted in ascending order using Lodash's _.sortBy() function. The natural order of the elements is used for sorting because no iterate is supplied. The elements are displayed in ascending order in the sorted array, which is logged into the console. In order to improve code readability and maintainability, this code demonstrates how Lodash makes sorting operations in JavaScript easier. It offers a practical method for sorting arrays with minimal effort. Example 2:Let us take another example to illustrate the Node.js lodash.sortBy() function. Output: Before sorting, the elements are: 4,8,2,z,a,g,28 After sorting, the elements are: 2,4,8,28,a,g,z Explanation:Using the variable "l", the Lodash library is imported in this snippet of Node.js code. An array a is constructed, which contains a combination of string and numeric elements [4, 8, 2, "z", "a", "g", "28"]. Using the console.log() method, the initial state of the array is logged to the console along with a newline character for improved formatting. After that, the elements of the array are sorted using Lodash's _.sortBy() method. However, because the elements are mixed types, Lodash sorts them according to their natural order, which might not arrange non-numeric values in the way that is intended. After sorting, the array is recorded to the console with the elements shown, once again formatted with a newline character. Example 3:Let us take another example to illustrate the Node.js lodash.sortBy() function. Output:
[
{ name: 'John', age: 30 },
{ name: 'Joseph', age: 35 },
{ name: 'Peter', age: 25 }
]
[
{ name: 'Peter', age: 25 },
{ name: 'John', age: 30 },
{ name: 'Joseph', age: 35 }
]
Explanation:The Lodash library, imported as _, is used in this Node.js code to sort an array of elements. Each object in the elements array represents a user and has properties for name and age. The "_.sortBy() function" is used to demonstrate two different sorting processes. The elements array is sorted using the name property of each object, and the result is stored in the sortingByName variable in the first operation. By using their names, the users are sorted alphabetically. Similar to the first operation, the second one sorts names in ascending order according to their ages by sorting the elements array by the age property, which is stored in the sortingByAge variable. In order to display the sorted user lists, both sorted arrays are finally recorded in the console. This code demonstrates how Lodash makes sorting easier by making it simple for developers to provide sorting criteria, which improves the code's readability and maintainability. Next TopicNodejs-net-socketaddress-method |
Node.js filehandle.readLines() Method
Prominent for its ability to efficiently manage asynchronous I/O operations, Node.js offers developers robust APIs for manipulating files and streams. The filehandle.readLines() function is one such API that was included in more recent versions. This approach simplifies and expedites the process of reading lines from...
4 min read
Difference between OAuth and SAML in Node.js
The modern web application development system is incomplete without integrating a module that handles authentication and authorization. Two widely used secure authentication and authorization protocols are OAuth and SAML. Both are mostly used for the same purposes, but they operate differently and have different applications....
5 min read
What is the relationship between Node.js and V8?
In this article, we will learn about the relationship between Node.js and V8. Before discussing their relationship, we must know about Node.js and V8. What is the Node.js? Node.js is a cross-platform, back-end, and JavaScript runtime environment using the V8 engine where the interprets and executes the...
4 min read
Difference between ORM and ODM in Node.js
In Node.js, two effective methods for interacting with databases are Object-Relational Mapping (ORM) and Object-Document Mapping (ODM). By enabling developers to interface with databases using objects in their code rather than raw SQL or NoSQL queries, they act as an abstraction layer that makes working with...
4 min read
Node.js fs.lchown() Method
In this article, we will discuss the fs.lchown() method in Node.js with its syntax, parameters, and examples. What is the Node.js fs.lchown() method? In Node.js, the fs.lchown() method is used to asynchronously modify the owner and group of a symbolic link without dereferencing it. This method receives a...
4 min read
Node JS fs.writeFile() Method
Node.js's event driven architecture and its non blocking operations made this significant player in the server side programming space. The fs (file system) is one of its important parts which gives developers essential tools to work with file system on their machine. Among these, fs.writeFile() method...
3 min read
Nock in Node.js
In Node.js development, Nock is the package that comes to be used for the generation of fake HTTP requests. It will help in easily mocking up API responses without having to get real access to the APIs. With Nock, we can create custom responses, and test...
3 min read
Node.js tlsSocket.enableTrace() Method
The tlsSocket.enableTrace() method is an inbuilt Application Programming Interface (API) in Node.js, specifically in the tls module. It is a client-side or server-side TLS (Transport Layer Security) connection, and it is a member of the TLSSocket class. The primary objective of tlsSocket.enableTrace() is to enable...
3 min read
Node.js Process unhandledRejection Event
In this article, we will discuss with its syntax, parameters, and examples. What is the ? The process is a global object in Node, and its information can be accessed by running the command process.info.js. This command keeps records and contains all the information about a...
4 min read
Scalability issues in Node.js
In node.js, a scalability issue refers to a limitation that ents the node.js application from working efficiently when a greater number of users are accessing that application. If the number of requests to the applications or data accessed by the users is huge and if databases...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India