How to sorting an array without using loops in Node.js ? Last Updated : 31 Aug, 2020 Comments Improve Suggest changes Like Article Like Report The setInterval() method repeats or re-schedules the given function at every given time-interval. It is somewhat like window.setInterval() Method of JavaScript API, however, a string of code can't be passed to get it executed. Syntax: setInterval(timerFunction, millisecondsTime); Parameter: It accepts two parameters which are mentioned above and described below: timerFunction <function>: It is the function to be executed.millisecondsTime <Time>: It indicates a period of time between each execution. The setTimeout() method is used to schedule code execution after waiting for a specified number of milliseconds. It is somewhat like window.setTimeout() Method of JavaScript API, however a string of code can't be passed to get it executed. Syntax: setTimeout(timerFunction, millisecondsTime); Parameter: It accepts two parameters which are mentioned above and described below: timerFunction <function>: It is the function to be executed. millisecondsTime <Time>: It indicates a period of time between each execution. Examples: Input: Array = [ 46, 55, 2, 100, 0, 500 ] Output: [0, 2, 46, 55, 100, 500] Input: Array = [8, 9, 2, 7, 18, 5, 25] Output: [ 2, 5, 7, 8, 9, 18, 25 ] Approach: The sorting requires visiting each element and then performing some operations, which requires for loop to visit those elements. Now here, we can use setInterval() method to visit all those elements, and perform those operations. The below code illustrates the above approach in JavaScript Language. File Name: Index.js javascript const arr = [46, 55, 2, 100, 0, 500]; const l = arr.length; var arr1 = []; var j = 0; var myVar1 = setInterval(myTimer1, 1); function myTimer1() { const min = Math.min.apply(null, arr); arr1.push(min); // arr[arr.indexOf(min)]=Math.max.apply(null, arr); arr.splice(arr.indexOf(min), 1); j++; if(j == l){ clearInterval(myVar1); console.log(arr1); } } Run Index.js file either on the online compiler or follow the following: node index.js Output: [0, 2, 46, 55, 100, 500] Comment More infoAdvertise with us Next Article How to sorting an array without using loops in Node.js ? amitkumarjee Follow Improve Article Tags : JavaScript Web Technologies Node.js Node.js-Misc Similar Reads How to search an element without using any loops in Node.js ? The setInterval() method repeats or re-schedules the given function at every given time-interval. It is somewhat like window.setInterval() Method of JavaScript API, however, a string of code canât be passed to get it executed. Syntax: setInterval(timerFunction, millisecondsTime); Parameter: It accep 3 min read How To Perform a Find Operation With Sorting In MongoDB Using Node.js? Performing a find operation with sorting in MongoDB using Node.js is a common task for developers working with databases. This guide will walk you through the process step-by-step, including setting up a MongoDB database, connecting to it using Node.js, performing a find operation, and sorting the r 3 min read How to Sort an Array of Objects with Null Properties in TypeScript ? Sorting an array of objects in TypeScript involves arranging objects based on specified properties. When dealing with null properties, ensure consistency by placing them either at the start or end of the sorted array to maintain predictability in sorting outcomes. Below are the approaches used to so 5 min read How to Sort an Array of Object using a Value in TypeScript ? Sorting an array of objects using a value in TypeScript pertains to the process of arranging an array of objects in a specified order based on a particular property or value contained within each object. The below approaches can be used to sort an array of objects using a value in TypeScript: Table 3 min read How To Sort An Array Without Mutating The Original Array In JavaScript? Sorting arrays is a common task in JavaScript, especially when you need to present data in a specific order. However, maintaining the original array is often necessary especially if the original data will be required later in your code. In this article we'll see how we can sort an array in JavaScrip 2 min read How to Sort an Array in TypeScript ? Array sorting is the process of arranging the elements within an array in a specified order, often either ascending or descending based on predetermined criteria. Below are the approaches used to sort an array in typescript: Table of Content Method 1: Using sort methodMethod 2: Spread OperatorMethod 3 min read How to Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met 5 min read How to sort an array of objects by property values ? In this article, we will try to understand how to sort an array of objects by property values in JavaScript with the help of certain examples. Pre-requisite: Array of Objects in JavaScript Example: Input:[ { name: "Ram", age: 17 }, { name: "Mohan", age: 30 }, { name: "Shyam", age: 15 }, { name: "Shy 4 min read How to sort an array of object by two fields in JavaScript ? We have given an array of objects and the task is to sort the array of elements by 2 fields of the object. There are two methods to solve this problem which are discussed below: Approach 1:First compare the first property, if both are unequal then sort accordingly.If they are equal then do the same 3 min read How to Return an Array from Async Function in Node.js ? Asynchronous programming in Node.js is fundamental for handling operations like network requests, file I/O, and database queries without blocking the main thread. One common requirement is to perform multiple asynchronous operations and return their results as an array. This article explores how to 4 min read Like