15
Apr

Perform sort with callback function in JavaScript

If you need to sort your Array in your javascript, you can use following code snippet:
————————————————————————————–

describe(“Functions to sort an Array via Callback() in JavaScript”, function(){

var myArray = [{
name: “x”,
age: 99
},
{
name: “b”,
age: 99
},
{
name: “c”,
age: 99
}
]

myArray.sort(function(number1, number2) {
if(number1.name > number2.name){
return 1;
}else{
return -1;
}
});

test(‘Expect true from return value of fetchData()’, ()=> {
console.log(myArray)
});

})

Output:
——-

● Console

console.log abc-test.js:28
[ { name: ‘b’, age: 99 },
{ name: ‘c’, age: 99 },
{ name: ‘x’, age: 99 } ]