Thursday, June 11, 2026
HomeLanguagesJavascriptHow to sorting an array without using loops in Node.js ?

How to sorting an array without using 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 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




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]

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS