使用特殊逻辑将已排序数组的数组元素移动到另一个位置

时间:2013-09-26 14:09:41

标签: javascript sorting object logic

我有一个像这样的排序集合:

 var sortedCollection = [
     {id: 3,x: 1,y: 1},
     {id: 2,x: 1,y: 2},
     {id: 1,x: 2,y: 1},
     {id: 9,x: 2,y: 2},
     {id: 5,x: 2,y: 3},
     {id: 8,x: 2,y: 4},
     {id: 7,x: 3,y: 1},
     {id: 6,x: 3,y: 2},
     {id: 4,x: 4,y: 1}
 ];

集合按x排序,y排序为第二排序值。

我的集合中的x和y值表示矩阵视图。使用示例模型,它看起来像这样:

[][][][]
[][][]
  []
  []

类似的结构reveal.js演示文稿使用,如果这更容易理解。

可以拖放元素([]代表一个元素)。 如果我将一个元素放到另一个元素上,我会显示一个指示器,其中放置了拖动的元素(上方,下方,之前,之后)。需要使用colllection中的数据来获取元素(缩略图)并将其发送回服务器

移动一个项目的功能应遵循以下逻辑: 我选了一个项目,例如{x: 2,y: 2},我想在集合中移动。第二项是放置该元素的参考,例如, {x: 3, y: 2}。应该可以将元素放在此元素的上方,下方,前后。

这意味着我的两个示例值{x: 2,y: 2}{x: 3, y: 2}引用了展示位置+以下信息:

  • “above”表示{x: 2,y: 2}变为{x: 3, y: 2},之后的所有值和具有相同x值的重复值{x: 3, y: 2}迭代一次
  • “下方”表示{x: 2,y: 2}变为{x: 3, y: 3},之后所有具有相同x值的值迭代一次

之前和之后的操作仅适用于y:1的值(示例{x: 2,y: 2}{x: 3, y: 1}

  • “之前”表示{x: 2,y: 2}变为{x: 3, y: 1}{x: 3, y: 1}变为{x: 4, y: 1},而x值为3的所有其他对象变为4所有元素x值由一个
  • 迭代
  • “after”表示{x: 2,y: 2}变为{x: 4, y: 1}{x: 3, y: 1}保持不变,但所有x值大于4的元素必须迭代一次

我希望我在描述我需要的东西时没有错。我无法管理它来处理每个填补空白或处理重复项以修复集合中的数字系列的情况。 在JavaScript中对此进行编程或简化此逻辑的任何提示都会有所帮助。

我创建了一个JSFiddle,它显示了主要的想法,并对我被卡住的位置做了一个todo评论: http://jsfiddle.net/FrontEnds/69PXk/8/

1 个答案:

答案 0 :(得分:0)

找到一个似乎有用的解决方案:

var sortedCollection = [
     {id: 3,x: 1,y: 1},
     {id: 2,x: 1,y: 2},
     {id: 1,x: 2,y: 1},
     {id: 9,x: 2,y: 2},
     {id: 5,x: 2,y: 3},
     {id: 8,x: 2,y: 4},
     {id: 7,x: 3,y: 1},
     {id: 6,x: 3,y: 2},
     {id: 4,x: 4,y: 1}
 ];

// example case
var  movedItemIndex = 3 // change this value to which item you want to move
    ,movedItem = sortedCollection[movedItemIndex]
    ,movedItemId = movedItem.id
    ,referenceItemIndex = 6 // change this value to which item reference where you want to move your item
    ,referenceItem = sortedCollection[referenceItemIndex]
    ,referenceItemId = referenceItem.id
    ,relativePlacing = "above"; // second reference indicator one of (above, below, before, after)

sortedCollection = moveSortedCollectionItem( sortedCollection, movedItemIndex, referenceItem.x, referenceItem.y, relativePlacing );
sortedCollection = fixSortCollectionsNumericalXAndYSequence( sortedCollection, movedItemId, referenceItemId );
console.dir( sortedCollection );

function moveSortedCollectionItem( sortedCollection, itemIndex, refPosX, refPosY, relativePlace )
{    
    switch( true )
    {
        case( relativePlace == "above" || relativePlace == "before" ):
            {
                sortedCollection[itemIndex].x = refPosX
                sortedCollection[itemIndex].y = refPosY
                break;
            }

        case( relativePlace == "below" ):
            {
                sortedCollection[itemIndex].x = refPosX
                sortedCollection[itemIndex].y = refPosY + 1
                break;
            }           

        case( relativePlace == "after" ):
            {
                sortedCollection[itemIndex].x = refPosX + 1
                sortedCollection[itemIndex].y = refPosY
                break;
            }
    }

    return sortedCollection;
}

function fixSortCollectionsNumericalXAndYSequence( sortedCollection, movedItemId, referenceItemId )
{    
    // make sure we have the right sort order
    sortedCollection.sort( sortByXAndY );

    // return sortedCollection;

    for( var i = 0, len = sortedCollection.length; i < len; i++ )
    {
        if( i == 0 && (sortedCollection[i].x != 1 || sortedCollection[i].y != 1) )
        {
            // first one must have x: 1 and y; 1 in order to make the whole thing work
            sortedCollection[i].x = 1;
            sortedCollection[i].y = 1;
        }
        else if( typeof sortedCollection[i-1] != 'undefined' /* has previous item */ )
        {
            sortedCollection[i] = _replaceXAndYFromItemWithPreviousItem( sortedCollection[i-1], sortedCollection[i] );
        }
    }

    return sortedCollection;
}

function _replaceXAndYFromItemWithPreviousItem( previousItem, currentItem )
{    
    // case: previous item x and current item x are the same
    if( currentItem.x == previousItem.x )
    {
        // set current item y = previous item y + 1
        currentItem.y = previousItem.y + 1;
    }  
    // case: current item x is not previous item x and not previous item x + 1
    else if( currentItem.x != previousItem.x && currentItem.x != previousItem.x + 1 )
    {
        // set current item x = previous item x + 1; set current item y = 1
        currentItem.x = previousItem.x + 1;                
        currentItem.y = 1;
    }

    return currentItem;
}

function sortByXAndY( model1, model2 )
{
    var  a = parseInt( model1.x )
        ,b = parseInt( model2.x )

    if( a == b )
    {
        var  c = parseInt( model1.y )
            ,d = parseInt( model2.y );

        return c - d;
    }

    return a - b;
}

http://jsfiddle.net/69PXk/17/