简体   繁体   English

任意重新排序和排序对象文字的javascript数组

[英]Arbitrarily reorder and sort javascript array of object literals

Good morning everyone! 大家,早安! I'm working through a problem this morning that entails reordering the following javascript array given the requirements I've crafted below: 我正在解决今天早上的一个问题,需要根据我在下面制定的要求重新排序以下javascript数组:

EDIT: I realize "reorder" and "sort" are sometimes used interchangeably. 编辑:我意识到“重新排序”和“排序”有时可以互换使用。 But in this context, I'm referring to 'reorder' as changing the 'order' property such that all objects have a unique order. 但在这种情况下,我将'重新排序'称为更改'order'属性,以便所有对象都具有唯一的顺序。 Sorting just refers to keeping the array in ascending order based on the order property. 排序仅指基于order属性按升序保持数组。

var toOrder = [
  {
    order: 1,
    visible: true
  },
  {
    order: 2,
    visible: true
  }
]
  1. Client code must be able to re-order this array, given a function that takes the object and a new order. 在给定获取对象和新订单的函数的情况下,客户端代码必须能够重新排序此数组。

  2. Array must stay sorted in ascending order (by the order property) at all times 数组必须始终按升序排序(按顺序属性)

  3. No two objects can have the same order 没有两个对象可以具有相同的顺序

I wrote some code that gets the job done, but I'm searching for a more eloquent solution. 我写了一些代码来完成工作,但我正在寻找一个更有说服力的解决方案。 My current algorithm for reorder (in psudocode) looks like this: 我目前的重新排序算法(在psudocode中)如下所示:

reorder(myObject, newOrder);
  1. Loop over toOrder array 循环到toOrder数组
  2. When matched object is found: 找到匹配的对象时:
  3. If new order is less than starting order 如果新订单少于启动订单
  4. Increment order property for objects between new order and starting order 新订单和起始订单之间的对象的增量订单属性
  5. If new order is greater than starting order 如果新订单大于启动订单
  6. Decrement order property between starting order and new order 减少起始订单和新订单之间的订单属性
  7. If new order is the same as old order, do nothing 如果新订单与旧订单相同,则不执行任何操作
  8. Set new order on matched object 在匹配对象上设置新订单
  9. sort() 分类()

The utility method "sort()" uses the native javascript sort() function to assure the array is ordered by the 'order' property at all times. 实用方法“sort()”使用本机javascript sort()函数来确保数组始终由'order'属性排序。 This works fine, but is there some more efficient/eloquent solution. 这很好,但有一些更有效/雄练的解决方案。

Thanks in advance. 提前致谢。

Your wording is a bit confusing, but here's how I'm interpreting it: 你的措辞有点令人困惑,但这就是我解释它的方式:

You have an array of objects, each with an order property. 您有一个对象数组,每个对象都有一个order属性。 This order property must be unique. 此订单属性必须是唯一的。 You wish to take Object A and place it in a new position, and re-apply the order properties so they are unique and sequential. 您希望将对象A放在一个新位置,然后重新应用订单属性,使它们是唯一且顺序的。

Here's how I would do it: 我是这样做的:

function reorder(object, newIndex) {

    var oldIndex = toOrder.indexOf(object);
    toOrder.splice(oldIndex, 1);
    toOrder.splice(newIndex, 0, object);

    // If you can guarantee your order properties were already indexed,
    // this loop could instead be
    // for (var i = Math.min(oldIndex, newIndex); i <= Math.max(oldIndex, newIndex); ++i)
    for (var i = 0; i < toOrder.length; ++i) {
        toOrder[i].order = i;
    }
}

Much simpler solution (still works for arbitrary order values, not only where newOrder is the actual array index): 更简单的解决方案(仍适用于任意order值,而不仅仅是newOrder是实际数组索引的位置):

  1. Set myObject.order to newOrder (where newOrder is some number between orders at the aim position). myObject.order设置为newOrder (其中newOrder是目标位置处的订单之间的某个数字)。 Eg -1 to move in front, or 1.5 to move between #1 and #2 例如-1在前面移动,或1.5#1#2之间移动
  2. Use the custom sort() 使用自定义排序()
  3. loop over the toOrder array and set the order of each item to its index (so that it becomes an integer again or is increased/decreased to match the new position) 循环遍历toOrder数组并将每个项的order设置为其索引(以便它再次变为整数或增加/减少以匹配新位置)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM