对数组排序,以避免重复属性的周边物品 [英] Sorting an array to avoid neighboring items having duplicate attributes

查看:116
本文介绍了对数组排序,以避免重复属性的周边物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的数组。每个对象有可能是红,蓝,黄,绿,橙色或紫色色彩属性。有数组中20-30对象,使颜色重复。我的目标是对该数组进行排序,以便没有颜色是彼此相邻。颜色的分布是不完全甚至,但接近。

I have an array of objects. Each object has a color attribute which could be "red", "blue", "yellow", "green", "orange" or "purple". There are 20-30 objects in the array so colors repeat. My goal is to sort the array so that no colors are next to each other. Distribution of colors is not exactly even but close.

这是我到目前为止所。它检查下一个和previous对象的色彩搭配,如果找到匹配它移动到数组的末尾。

This is what I have so far. It checks the next and previous object for a color match and if it finds a match it moves it to the end of the array.

private function sortColors():void
{
    var getNext:uint;
    var getPrev:uint;
    var maxCount:uint = colorArray.length;
    for (var i:uint = 0; i < maxCount; i++) {
        var cur:ValueObject = colorArray[i];
        (i == maxCount-1) ? getNext = 0 : getNext = i+1;
        (i == 0) ? getPrev = maxCount-1 : getPrev = i-1;
        var next:ValueObject = colorArray[getNext];
        var prev:ValueObject = colorArray[getPrev];
        if (cur.color == next.color) {
            var move:ValueObject = colorArray[getNext];
            colorArray.splice(getNext, 1);
            colorArray.push(move);
        }
        if (cur.color == prev.color) {
            var move:ValueObject = colorArray[getPrev];
            colorArray.splice(getPrev, 1);
            colorArray.push(move);
        }
    }
}

这工作正常,但如果有更多的是一种特定的颜色,他们最终在重复结束。我可以添加一些东西到最后抛出的回混,但我觉得必须有一个更好的办法。有人开导我。

This works OK but if there is more of a certain color they end up repeating at the end. I could add something to the end to throw those back into the mix but I feel like there must be a better way. Someone enlighten me.

推荐答案

尝试:

var colorObjects:Array = [/* list of objects with colors - populated below*/];
var jumbled:Array = [];

var lastColor:String = "";
function getDifferentTile():void
{
    if(lastColor.length == 0)
    {
        jumbled.push(colorObjects.pop());
        lastColor = jumbled[0].mycolor;
    }
    else
    {
        var i:Object;
        for each(i in colorObjects)
        {
            var repeat:uint = 0;
            if(i.mycolor != lastColor)
            {
                jumbled.push(i);
                lastColor = i.mycolor;

                colorObjects.splice(colorObjects.indexOf(i), 1);

                return;
            } else {
               repeat++;
            }
            if (repeat > 0 && repeat == colorObjects.length) {
               jumbled.push(i);
               colorObjects.splice(colorObjects.indexOf(i), 1);
               return;
            }
        }
    }
}

// list of random colors
var colors:Array = ["0x000000","0x444444","0xFFFFFF","0xFF00FF"];

// prepare random array for test
var i:uint = 0;
for(i; i<100; i++)
{
    var obj:Object =
    {
        mycolor: colors[uint(Math.random()*colors.length)]
    };

    colorObjects.push(obj);
}


// fill the jumble array until the original listing is empty
while(colorObjects.length > 0)
{
    getDifferentTile();
}


// output jumbled
var j:Object;
for each(j in jumbled)
{
    trace(j.mycolor);
}

这篇关于对数组排序,以避免重复属性的周边物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆