Introduction
In this article we will learn about how to short the Array of Objects using javascript. Sort an object collection in javascript.
Learn NodeJs with Basic Example . Start with NodeJs
The sort() method sorts an array alphabetically. By default, the sort() function sorts values as strings.
When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
You may also interested Difference between array and ArrayList with example.
In this article we will learn about how to short the Array of Objects using javascript. Sort an object collection in javascript.
Learn NodeJs with Basic Example . Start with NodeJs
The sort() method sorts an array alphabetically. By default, the sort() function sorts values as strings.
var Student = [
{ Name: 'Nicks', Address: 'Delhi', RN: 2 },
{ Name: 'Bob', Address: 'US', RN: 4 },
{ Name: 'JP', Address: 'UK', RN: 1 }
];
function compare(a, b) {
const RN_A = a.RN;
const RN_B = b.RN;
let comparison = 0;
if (RN_A > RN_B) {
comparison = 1;
} else if (RN_A < RN_B) {
comparison = -1;
}
return comparison;
}
console.log(Student.sort(compare));
|
You may also interested Difference between array and ArrayList with example.
0 comments:
Post a Comment