基于javascript中的另一个数组排序数组

时间:2017-06-26 07:45:25

标签: javascript arrays sorting ecmascript-6

我有两个数组:

const originalArray = ['a', 'n', 'u', 'b', 'd', 'z'];

const sortOrder = ['n', 'z'];

所以我希望输出为['n', 'z', 'a', 'u', 'b', 'd'];

基本上是originalArray的顺序,按secondArray的顺序排序。

我可以基于第二个数组弹出原始数组中的元素,然后可以将它们添加到前面,这将为我提供所需的解决方案,但不确定这是否有效或是否有更好的方法来使用它array.sort(fxn);

const originalArray = ['a', 'n', 'u', 'b', 'd', 'z'];

const sortOrder = ['n', 'z'];
const reverseOrder = sortOrder.reverse();
for (let elem of reverseOrder) {
const indexofelem = originalArray.indexOf(elem);
 originalArray.unshift(originalArray.splice(indexofelem, 1)[0]);
}

console.log(originalArray);

4 个答案:

答案 0 :(得分:2)

您可以根据sortOrder数组中的匹配索引创建排序函数。

const originalArray = ['a', 'n', 'u', 'b', 'd', 'z'];

const sortOrder = ['n', 'z'];

function sortArrays(a, b) {
  var indexOfA = sortOrder.indexOf(a),
    indexOfB = sortOrder.indexOf(b);

  if (indexOfA == -1) {
    indexOfA = sortOrder.length + 1;
  }
  if (indexOfB == -1) {
    indexOfB = sortOrder.length + 1;
  }

  if (indexOfA < indexOfB) {
    return -1;
  }

  if (indexOfA > indexOfB) {
    return 1;
  }

  return 0;
}

originalArray.sort(sortArrays);

console.log(originalArray);
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

答案 1 :(得分:2)

您可以使用对象作为项目的位置,或者使用默认值零来计算增量。

如果delta为零,则不会授予稳定的排序。

const array = ['a', 'n', 'u', 'b', 'd', 'z'],
      sortOrder = ['n', 'z'],
      order = sortOrder.reduce((r, a, i, aa) => (r[a] = -aa.length + i, r), {});

array.sort((a, b) => (order[a] || 0) - (order[b] || 0));
console.log(array);
console.log(order);
.as-console-wrapper { max-height: 100% !important; top: 0; }

对于稳定排序,您可以将sorting with map与对象一起使用,这会使值的索引和组保持优先级。在组内部,排序顺序由原始数组的索引维护。

// the array to be sorted
var list = ['a', 'n', 'u', 'b', 'd', 'z'],
    sortOrder = ['n', 'z'],
    order = sortOrder.reduce((r, a, i, aa) => (r[a] = -aa.length + i, r), {});

// temporary array holds objects with position and sort-value
var mapped = list.map(function (el, i) {
    return { index: i, group: order[el] || 0 };
});

// sorting the mapped array containing the reduced values
mapped.sort(function (a, b) {
    return a.group - b.group || a.index - b.index;
});

// container for the resulting order
var result = mapped.map(function (el) {
    return list[el.index];
});

console.log(result);

答案 2 :(得分:2)

对于大数据数组处理,我们可以使用对象映射

var originalArray = ['a', 'n', 'u', 'b', 'd', 'z'],
sortOrder = ['n', 'z'];
var result = {};
var finalResponse = [];

// loop over item array which have to sort
originalArray.forEach(function(elem) {
  // if element not persent in result object then create map with true flag set
  if(!result[elem]){
    result[elem] = true
  }
});

// loop over sort order to check element exist in given array
sortOrder.forEach(function(elem) {
  //if element exist then push to array data and set flag to false for element matched.
  if(result[elem]){
    finalResponse.push(elem);
    result[elem] = false
  }
});

// loop over final object data and find all element with true value
for(var key in result) {
  if(result[key]){
   finalResponse.push(key);
  }
}
console.log('final response ',finalResponse);

答案 3 :(得分:1)

如果您需要稳定的排序(即您希望不在sortOrder数组中的元素保持其原始顺序),您可以使用Object.assign和偏移量组合两个排序地图。

因此,为了确保我们的排序稳定,我们将两个地图组合在一起:   - 原始索引的映射,从数据长度开始,到1   - 已定义索引的映射,偏移数据长度

&#13;
&#13;
// Create a map that holds an integer sort index for 
// each value in an array based on its index
const sortMap = (ref, offset = 0) => 
  ref.reduce((map, x, i) =>
    Object.assign(map, { [x]: (ref.length - i) + offset })
  , {});

// Returns a function that sorts based on a value in a map
const sortWithMap = map => (a, b) => 
  (map[b] || 0) - (map[a] || 0);



const originalArray = "abcdefghijlmnopqrstuvwxyz".split("");
const sortOrder = ['n', 'z'];


const sortToOrder = (order, data) => data.sort(
  sortWithMap(sortMap(order))
);


const sortToOrderStable = (order, data) => data.sort(
  sortWithMap(Object.assign(
      sortMap(data),
      sortMap(order, data.length)
  )));

console.log("Stable:",
  JSON.stringify(
    sortToOrderStable(sortOrder, originalArray)
  )
);

console.log("Default:",
  JSON.stringify(
    sortToOrder(sortOrder, originalArray)
  )
);
&#13;
&#13;
&#13;