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;
但我不知道如何按最高数字对它们进行排序。
答案 0 :(得分:0)
如果你给我person对象的代码,我可以给你确切的代码,但你可以做这样的事情
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);
你需要调用SortByPoint函数
function SortByNumber(a, b) {
var pointA = a.likes;
var pointB = b.likes;
return ((pointA > pointB) ? -1 : ((pointA < pointB) ? 1 : 0));
}
修改强>
已更新以满足更新的问题
答案 1 :(得分:0)
按升序排列:
array.sort(function(a, b){ return a.likes-b.likes})
下降
array.sort(function(a, b){ return b.likes-a.likes})
答案 2 :(得分:0)
用于降序排序usep>
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;
}
}
对于升序排序,请使用
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;
}
}