我有数组中的卡片,并且有排序按钮,但我不知道如何进行排序,例如钻石,梅花,黑桃,红心卡片想要与此卡片分开..
var aList:Array =
[
{card:Globe.self.realstage.joker_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.king_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.queen_mc, x:45.85, y:213.95},
{card:Globe.self.realstage.a_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.ten_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.five_mc, x:45.85, y:213.95},
{card:Globe.self.realstage.two_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.nine_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.four_mc, x:45.85, y:213.95},
];
有谁知道可以详细说明一下吗?谢谢
最佳答案
我建议添加一些额外的参数,比如“重量”:
var aList:Array =
[
{card:Globe.self.realstage.joker_mc, x:605.55, y:195.45, weight: 11},
{card:Globe.self.realstage.king_mc, x:323.80, y:298.45, weight: 13},
{card:Globe.self.realstage.queen_mc, x:45.85, y:213.95, weight: 12},
{card:Globe.self.realstage.a_mc, x:605.55, y:195.45, weight: 14},
{card:Globe.self.realstage.ten_mc, x:323.80, y:298.45, weight: 10},
{card:Globe.self.realstage.five_mc, x:45.85, y:213.95, weight: 5},
{card:Globe.self.realstage.two_mc, x:605.55, y:195.45, weight: 2},
{card:Globe.self.realstage.nine_mc, x:323.80, y:298.45, weight: 9},
{card:Globe.self.realstage.four_mc, x:45.85, y:213.95, weight: 4},
];
然后根据这个权重对数组进行排序:
// in descending order
aList.sort(function (c1:Object, c2:Object):int
{
if (c1.weight > c2.weight) return -1;
if (c1.weight < c2.weight) return 1;
return 0;
});
// in ascending order:
aList.sort(function (c1:Object, c2:Object):int
{
if (c1.weight > c2.weight) return 1;
if (c1.weight < c2.weight) return -1;
return 0;
});
如果您无法更改对象(或者由于某种原因您不想在其中添加权重),您可以创建一个外部辅助函数:
// somewhere
function getWeight(data: Object):int {
switch(data.card) {
case Globe.self.realstage.two_mc:
return 2;
case Globe.self.realstage.four_mc:
return 4;
...
default: return 0;
}
}
aList.sort(function (c1:Object, c2:Object):int
{
if (getWeight(c1) > getWeight(c2)) return 1;
if (getWeight(c1) < getWeight(c2)) return -1;
return 0;
});
关于arrays - 如何在 ActionScript 3.0 中对钻石、梅花、黑桃、红心等卡片进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44173956/