如何排序卡,如钻石,俱乐部,铲子,动作心脏3.0
问题描述:
我有卡阵列,我有按钮的排序,但我不知道如何做钻石,俱乐部,锹,心脏卡想要分开从该卡..如何排序卡,如钻石,俱乐部,铲子,动作心脏3.0
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},
];
任何人都知道,你可以请详细说明这个one.Thank你
答
我建议增加一些额外的参数,说“重”:
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;
});