Home >Web Front-end >JS Tutorial >Javascript custom sorting node running example_javascript skills

Javascript custom sorting node running example_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:32:401044browse

[javascript]

Copy code The code is as follows:

var studs = [{name: 'clap ', age: 15},
{name: 'martin', age: 13},
{name: 'merry', age: 10}];

function sortbyage(a, b )
{
// Sort by age from smallest to largest
return a.age > b.age;
}

function sortbyname(a, b)
{
return a.name > b.name;
}

//Sort by age
studs.sort(sortbyage);
for(var i = 0; i < ; studs.length; i )
console.log(studs[i]);

//Sort by name
studs.sort(sortbyname);
for(var i = 0 ; i < studs.length; i )
console.log(studs[i]);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn