使用对象的多个键和自定义排序逻辑对数组进行排序

时间:2011-11-02 00:17:42

标签: javascript arrays sorting

我有一个看起来像的对象:

var object = [
    {"begin":0,  "end":20}, 
    {"begin":30, "end":300},
    {"begin":40, "end":60}, 
    {"begin":40, "end":50},
    {"begin":80, "end":100},
    {"begin":80, "end":100},
    {"begin":350,"end":370}
]

我想迭代这个对象并按以下形式对其进行排序:

0   to 20   -> start here
30  to 300  -> next smallest number from 20 is 30
350 to 370  -> start from previous `end`, next number after 300 is 350.

// now, since there is no larger number than 370, start loop again.
40  to 60   -> start from 40 since this is the smallest unused number
80  to 100  -> next unused smallest number from 60 is 80

// now, since there is no larger unused number than 100, start loop again.
40  to 50
80  to 100

返回相同的数组或新数组都可以。

任何帮助总是受到赞赏。

3 个答案:

答案 0 :(得分:1)

请参阅此处的工作示例:http://jsfiddle.net/bXNLT/2/

function sortItems(items) {
    items = items.slice() // make a copy
        // sort by "begin", in case the 
        // data isn't pre-sorted
        .sort(function(a,b) {
            return a.begin - b.begin;
        });
    var sorted = [], 
        idx = 0, 
        item;
    while (items.length) {
        // move the current item into the sorted array
        item = items.splice(idx, 1)[0];
        sorted.push(item);
        // find next index
        for (; idx<items.length; idx++) {
            if (items[idx] && items[idx].begin > item.end) {
                break;   
            }
        }
        // reset to 0 if we went too far
        idx = idx < items.length ? idx : 0;
    }
    return sorted;
}

用法:

var arr = [
    {"begin":0,  "end":20}, 
    {"begin":30, "end":300},
    {"begin":40, "end":60}, 
    {"begin":40, "end":50},
    {"begin":80, "end":100},
    {"begin":80, "end":100},
    {"begin":350,"end":370}
];
sortItems(arr); // sorted as you specify

答案 1 :(得分:0)

对于你想要做的事情,正常排序(即使使用自定义功能)可能不适用于整个事情。

但是,您应该使用它来使元素处于良好的顺序以便进一步处理。首先按开头排序,然后按结束排序。

然后,如果为每个数据集添加“脏”标志,则会变得更容易。

var obj = [
    {"begin":0,  "end":20,   "dirty": false}, 
    {"begin":30, "end":300,  "dirty": false},
    {"begin":40, "end":60,   "dirty": false}, 
    {"begin":40, "end":50,   "dirty": false},
    {"begin":80, "end":100,  "dirty": false},
    {"begin":80, "end":100,  "dirty": false},
    {"begin":350,"end":370,  "dirty": false}
];

function getNextSet(arr)
{
  var ret = [];
  var lastEnd = -1;

  for (var i = 0; i < arr.length; i++)
  {
     if (arr[i].begin >= lastEnd && arr[i].dirty == false)
     {
        ret.push(arr[i]);
        arr[i].dirty = true;
        lastEnd = arr[i].end;
     }
  }

  return ret;
}

// do custom sorting as mentioned in the other answer to get in a good starting state

var set = [];
do {
  set = getNextSet(obj);
  // do whatever
} while (set.length != 0);

答案 2 :(得分:-1)

Javascript数组有sort方法,可以将比较函数作为参数:

var data = [...]; // your data here
data.sort(function(a, b) {
    // insert your logic here
    // return values:
    //  -1 (a less than b)
    //   0 (a equal to b)
    //   1 (a greater than b)
});

此方法有更多示例on this site