按字段值排序数组=== 0

时间:2016-06-10 08:49:31

标签: javascript underscore.js

我有一些以下列方式构建的数据:

theArray = [

    0:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 5,
            finished : 7
        }
    ],
    1:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 0,
            finished : 3
        }
    ],
    2:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 3,
            finished : 1
        }
    ],
    3:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 0,
            finished : 7
        }
    ],
    4:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 4,
            finished : 0
        }
    ]

]

我想通过theArray重新排列status.started内的数组,以便将status.started === 0的任何数组推到底部。所以theArray看起来像这样:

theArray = [

    0:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 5,
            finished : 7
        }
    ],
    1:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 3,
            finished : 1
        }
    ],
    2:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 4,
            finished : 0
        }
    ],
    3:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 0,
            finished : 3
        }
    ],
    4:[
        0 : {},
        1 : {},
        2 : {},
        3 : {},
        status : {
            started : 0,
            finished : 7
        }
    ]
]

如果可能,我最好喜欢使用Underscore.js。它永远不会让我失望!

5 个答案:

答案 0 :(得分:1)

这将是underscore.js的开始: 仅用于将status.started元素移动到底部。

_.sortBy(theArray, function(element) {
    return element.status.started === 0 ? 0 : 1;
});

答案 1 :(得分:1)

由于您只想根据值=== 0或不是进行排序,您可以指定默认值并进行比较。

theArray.sort(function(a, b) {
  var _v1 = a.status.started === 0 ? 1 : -1;
  var _v2 = b.status.started === 0 ? 1 : -1;
  return _v1 < _v2 ? -1 : _v1 > _v2 ? 1 : 0;
});

答案 2 :(得分:1)

很好的问题,但在这种情况下排序是没有必要的,因为我们没有排序任何东西。这只是将黑羊移到生产线末端的业务。那么为什么不把它从它所在的位置拼接出来并推到最后。但随后指数转移,你的forEach循环变得有趣。好的,这就是为什么我们有一个很好的reduceRight()供我们玩。您删除当前索引位置项并将其拍摄到最后...但下一项仍然是相同的下一项。凉。我们来做吧。

var theArray = [{ 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 5, finished: 7 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 0, finished: 3 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 3, finished: 1 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 0, finished: 7 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 4, finished: 0 } }];
theArray.reduceRight((p,c,i,a) => !c.status.started && a.push(a.splice(i,1)[0]));
console.log(theArray);

就是这样。仅在O(n)时间。

答案 3 :(得分:0)

试试这个。你的哈希在语法上是不正确的,所以我冒昧地解决了这个问题。之后,您可以使用。

    theArray.sort(function(a,b) {
         return b.status.started - a.status.started;
    })

这是带有固定输入的整个代码。

    theArray = [

        {
            0 : {},
            1 : {},
            2 : {},
            3 : {},
            status : {
                started : 5,
                finished : 7
            }
        },
        {
            0 : {},
            1 : {},
            2 : {},
            3 : {},
            status : {
                started : 0,
                finished : 3
            }
        },
        {
            0 : {},
            1 : {},
            2 : {},
            3 : {},
            status : {
                started : 3,
                finished : 1
            }
        },
        {
            0 : {},
            1 : {},
            2 : {},
            3 : {},
            status : {
                started : 0,
                finished : 7
            }
        },
        {
            0 : {},
            1 : {},
            2 : {},
            3 : {},
            status : {
                started : 4,
                finished : 0
            }
        },
    ]

    theArray.sort(function(a,b) {
         return b.status.started - a.status.started;
    })
    console.log(theArray);

答案 4 :(得分:0)

对于未启动的项目,您可以采用默认值Infinity

var theArray = [{ 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 5, finished: 7 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 0, finished: 3 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 3, finished: 1 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 0, finished: 7 } }, { 0: {}, 1: {}, 2: {}, 3: {}, status: { started: 4, finished: 0 } }];

theArray.sort(function (a, b) {
    return (a.status.started || Infinity) - (b.status.started || Infinity);
});

console.log(theArray);