如何在ActionScript 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},
            ];

任何人都知道你能详细说明一下吗?谢谢

我建议添加一些额外的参数,比如"权重":

 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;
    });
你可以

像@Nbooo所说的那样添加额外的参数,并使用SortSortField,如下所示:

var sortField : SortField = new SortField();
sortField.name = "weight";
sortField.numeric = true;
var sort: Sort = new Sort();
sort.fields = [sortField];
this.aList.sort = sort;
this.aList.refresh();

参考这里。

最新更新