0
  var person = function(name,video,twitter,facebook,number){
     this.name=name;
     this.video=video;
     this.twitter=twitter;
     this.facebook=facebook;
      this.likes= number;
     this.dislike=0;
     this.addlike=addlike;
     this.adddislike=adddislike;
    var x=0;
function addlike(){var cap = x +=1; this.likes= cap;}

function adddislike(){var cap = x +=1; this.dislike = cap;}

}

这些是我使用对象构造函数创建的对象:

 var  nana =  new person("Shirley","G-ma Stuff", "shirley Tweet","shirley face",100);
 var  rj = new person("Ronald ", "java", "Ronald Tweet","Ronald" , 72);
 var  tori = new person( "Toir ", "Cars","mom tweet","mom face",48);
 var ronald = new person("Ronald","Bear","Ronald Twitter","Ronald Facebook",12);

这是我在数组中创建并分配位置的数组:

 var array = [];
 array[0]=ronald;
 array[1]=tori;
 array[2]=rj;
 array[3]=nana;

但我不知道如何按最高数字对它们进行排序。

4

3 回答 3

0

If you give me the code for the person object, i can give you the exact code but you can do something like this

var nana = new person("Shirley", "G-ma Stuff", "shirley Tweet", "shirley face", 100);
var rj = new person("Ronald ", "java", "Ronald Tweet", "Ronald", 72);
var tori = new person("Toir ", "Cars", "mom tweet", "mom face", 48);
var ronald = new person("Ronald", "Bear", "Ronald Twitter", "Ronald Facebook", 12);

var asdf = [nana, rj, tori, ronald];
asdf.sort(SortByPoint);

and you need function call SortByPoint

function SortByNumber(a, b) {
    var pointA = a.likes;
    var pointB = b.likes;

    return ((pointA > pointB) ? -1 : ((pointA < pointB) ? 1 : 0));
}

EDIT:

Updated to meet updated question

于 2013-07-31T05:49:28.347 回答
0

按升序排列:

array.sort(function(a, b){ return a.likes-b.likes})

在下降

array.sort(function(a, b){ return b.likes-a.likes})
于 2013-07-31T06:08:07.897 回答
0

工作演示在这里

用于去发分拣使用

 function sortData(collection) {
        collection.sort(compare);
        function compare(a, b) {
            if (a.likes < b.likes)
                return 1;
            if (a.likes > b.likes)
                return -1;
            return 0;
        }
    }

对于 assending 排序使用

function sortData(collection) {
        collection.sort(compare);
        function compare(a, b) {
            if (a.likes < b.likes)
                return -1;
            if (a.likes > b.likes)
                return 1;
            return 0;
        }
    }
于 2013-07-31T06:21:30.937 回答