-4

我有这个代码

function Human(firstName,lastName,age)
{
    this.firstName = firstName;
    this.lastName =  lastName;
    this.age = age;
}

var firstHuman = new Human("Ivan","Ivanov",25);
var secondHuman = new Human("Petar","Petrov",34);
var thirdHuman = new Human("Ivailo", "Stoyanov",28);
var forthHuman = new Human("Petar", "Tudjarov",50);
var fifthHuman = new Human("Hari", "Qvorov",20);

var Humans = new Array (firstHuman,secondHuman,thirdHuman,forthHuman,fifthHuman);

我如何按他们的名字对它们进行排序,以便让 FifthHuman(H 是字母表中最小的)在数组中排在第一位

4

1 回答 1

3
Humans.sort(function(a, b){return a.firstName > b.firstName});

http://jsfiddle.net/3wyBc/

于 2013-03-28T16:33:12.493 回答