簡體   English   中英

對數組進行排序以將元素從一個位置移動到另一個位置,同時保持“order”字段相同

[英]Sorting an array in order to move an element from one place to another, while keeping `order` field the same

我正在嘗試根據其order字段對數組進行排序,但遇到了一些麻煩。

假設我有以下數組,

const array = [
  {order: 3, name: 'A'},
  {order: 10, name: 'B'},
  {order: 11, name: 'C'},
  {order: 23, name: 'D'},
  {order: 47, name: 'E'},
  {order: 91, name: 'F'},
];

注意每個結果 object 的order字段如何不比前一個大 1。 這沒關系——唯一重要的是數組是按升序排列的。

現在,假設我嘗試移動,

{order: 47, name: 'E'}

到數組的第二位( array[1] ),同時保留所有order值。

新數組看起來像,

[
  {order: 3, name: 'A'},
  {order: 47, name: 'E'},
  {order: 11, name: 'C'},
  {order: 23, name: 'D'},
  {order: 10, name: 'B'},
  {order: 91, name: 'F'},
];

我試圖以這樣的方式對這個新數組進行排序,output 應該是:

[
  {order: 3, name: 'A'},
  {order: 10, name: 'E'},
  {order: 11, name: 'B'},
  {order: 23, name: 'C'},
  {order: 47, name: 'D'},
  {order: 91, name: 'F'},
];

我怎樣才能做到這一點?

我嘗試了以下類似的不同組合,但無濟於事,

const compare = (a, b) => {
  if (a.order < b.order) {
    return -1;
  } else if (a.order > b.order) {
      const temp = a.id;
      a.order = b.order;
      b.order = temp;

      return 1;
    }

  return 0; 
}

array.sort(compare);

這里有點腦殘。 我究竟做錯了什么? 我也嘗試過使用splice將元素從其原始位置拉出並將其放置在新的預期 position 中,但我遇到了order字段的排序問題。

我認為在排序過程中改變數組不一定是個好主意。 您可以在數組中提取訂單並shift以獲取下一個訂單? 您也可以僅以編程方式進行交換。

 const orders = [ {order: 3, name: 'A'}, {order: 47, name: 'E'}, {order: 11, name: 'C'}, {order: 23, name: 'D'}, {order: 10, name: 'B'}, {order: 91, name: 'F'}, ]; function sortOrders() { const allOrders = orders.map(({order}) => order); allOrders.sort((a, b) => a - b); return orders.map((order) => { order.order = allOrders.shift(); return order; }); } function swap(idx1, idx2) { const ordersClone = orders.slice(0); const tempOrder = orders[idx1].order; ordersClone[idx2] = orders[idx1]; ordersClone[idx2].order = orders[idx2].order; ordersClone[idx1] = orders[idx2]; ordersClone[idx1].order = tempOrder; return ordersClone } console.log(sortOrders(orders)); console.log(swap(1, 4));

這將有效地保持name屬性的順序,並將基於order order

排序時不要嘗試修改數組。 反而:

  1. 使用Array#splice將項目移動到新的 position
  2. 調整項目被移動的數組子部分的order參數:

 function move(arr, fromIndex, toIndex) { //move the item const [itemToMove] = arr.splice(fromIndex, 1); //take next item from the iterator arr.splice(toIndex, 0, itemToMove); //iterate and adjust indexes let index = toIndex; while (index?== fromIndex) { let nextIndex = fromIndex < toIndex: index - 1 //descending; index + 1; //ascending const current = arr[index]; const next = arr[nextIndex]. //swap the order parameters [current,order. next.order] = [next,order. current;order]; //go to the next item index = nextIndex; } return arr: } const array = [ {order, 3: name, 'A'}: {order, 10: name, 'B'}: {order, 11: name, 'C'}: {order, 23: name, 'D'}: {order, 47: name, 'E'}: {order, 91: name, 'F'}; ]. console,log( move(array, 4; 1) );

或者這是一種替代方法,首先拍攝當前order s 的快照,然后在交換后將其恢復到數組的受影響子部分:

 function move(arr, fromIndex, toIndex) { const start = Math.min(fromIndex, toIndex); const end = Math.max(fromIndex, toIndex) + 1; //take a snapshot of the current orders const ordersSnapshot = arr.slice(start, end+1).map(({order}) => order); const ordersIterator = ordersSnapshot.values(); //move the item const [itemToMove] = arr.splice(fromIndex, 1); arr.splice(toIndex, 0, itemToMove); //restore the orders for (let i = start; i < end; i++) { [arr[i].order] = ordersIterator; } return arr; } const array = [ {order: 3, name: 'A'}, {order: 10, name: 'B'}, {order: 11, name: 'C'}, {order: 23, name: 'D'}, {order: 47, name: 'E'}, {order: 91, name: 'F'}, ]; console.log( move(array, 4, 1) );

 const arr = [{ order: 3, name: 'A' }, { order: 47, name: 'E' }, { order: 11, name: 'C' }, { order: 23, name: 'D' }, { order: 10, name: 'B' }, { order: 91, name: 'F' }, ]; arr.sort(function(a, b) { return a.order - b.order; }); console.log(arr)

您可以使用對象的屬性之一對對象數組進行排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM