以相同的方式对多个数组进行排序Javascript

Sorting Multiple Arrays the Same Way Javascript

本文关键字:数组 排序 Javascript 方式      更新时间:2023-09-26

我想做的是对这些数组的元素进行排序:

name[];
standing[];
value[];
maxvalue[];
id[];
repstring[];

通过排序数组:

var SortArr = [1,2,4,6,3,5,7,8,12,11,10,9...etc]

一直到106,这是这些阵列的长度。

这个排序数组是一个以自定义方式重新排列数据的键,而不是按字母顺序排列。

有什么方法可以有效地做到这一点?当我读到这篇文章时,我已经考虑过将上面的数组组合成一个对象,这可能是最好的方法(?),但我不知道如何做到。我对javascript和编码很陌生,所以欢迎使用任何指针。感谢

这是我下面的代码:

}
//...
//this is all within a function called rep()

 //Declare things
  var name = [ ];
  var standing = [ ];
  var value = [ ];
  var maxvalue = [ ];
  var id = [ ];
  //For converting repstanding to strings
  var reputation = new Array ("Hated", "Hostile", "Unfriendly", "Neutral", "Friendly", "Honored", "Revered", "Exalted");
  //array holding converted strings for each faction
  var repstring = [ ];
  var count = toon.reputation.length; //this value is 106
  //Populate arrays
  for (i = 0; i < count; i++)
  {
    name[i] = toon.reputation[i].name; //string
    standing[i] = toon.reputation[i].standing; //number from 0-7
    value[i] = toon.reputation[i].value; // number from 0-21000
    maxvalue[i] = toon.reputation[i].max; //number from 3000-21000
    id[i] = toon.reputation[i].id //number from 1-1741
  }
  //Convert standing numbers to reputation string
  for (i=0; i < count; i++)
  {
    repstring[i] = reputation[standing[i]];
  }
  //Create output array
  var repInfo = new Array(
  name, standing, repstring, value, maxvalue, id, SortArr
  )
  //Output array
return repInfo;
}

编辑:这是我的函数输出的示例:(注意:这只显示每个阵列的值1-5,每个阵列中有106个值)

Name                Standing    Repstring   Value   MaxValue    ID      SortArr
The Black Prince        3       Neutral     2800    3000        1359        5
The Sons of Hodir       0       Hated       0       36000       1119        3
Booty Bay               3       Neutral     1875    3000        21          2
Zandalar Tribe          3       Neutral     0       3000        270         1
The Oracles             3       Neutral     0       3000        1105        4

好的,所以我想做的是重新排列这些,使它们按SortAr排序,所以它看起来像这样:

Name                Standing    Repstring   Value   MaxValue    ID      SortArr
Zandalar Tribe          3       Neutral     0       3000        270         1
Booty Bay               3       Neutral     1875    3000        21          2
The Sons of Hodir       0       Hated       0       36000       1119        3
The Oracles             3       Neutral     0       3000        1105        4
The Black Prince        3       Neutral     2800    3000        1359        5

根据您上面所说的内容,我将执行以下操作。。。

var SortArr = [1, 2, 4, 6, 3, 5, 7, 8, 12, 11, 10, 9];
var collatedArray = [];
for (var i = 0; i < SortArr.length; i++) {
    index = SortArr[i];
    var collated = {
        name : name[index],
        standing : standing[index],
        value : value[index],
        maxvalue : maxvalue[index],
        id : id[index],
        repstring : repstring[index]
    }
    collatedArray.push(collated);
}
// Do what you want now with this collated array,
// which is an array of the objects you require.
// You can loop through them and access the properties
// via obj.id, obj.repstanding, etc.

例如

function getPlayer(id) {
    var player = null;
    for (var i = 0; i < collatedArray.length; i++) {
        if (collatedArray[i].id == id) {
            player = collatedArray[i];
            break;
        }
    }
    return player;
}

然后只访问玩家对象的属性。。。

player.id
player.repstanding
// etc...

如果您需要返回的对象是不可变的,那么修改此代码以从对象中删除公共变量并允许通过getter方法进行访问就足够简单了。

你真的应该根据你的模型来思考,并存储一个玩家数组(或者上面这些东西代表的任何东西),而不是为特定模型的每个属性存储数组。

如果我正确理解了这个问题,你想在你传递给排序的比较器函数中使用SortArray中的数字索引。

// indices:             0  1  2  3  4  5  6
// When you see a 1, treat it like 0 (its index in array) in the comparator function
// When you see a 2, treat it like 2 (its index in array)         
var desiredSortOrder = [1, 3, 2, 7, 5, 4, 6];
var toSort = [1, 7, 2, 3, 4, 5, 6, 1, 2, 3, 7, 4, 5, 6];
// Map where the key is the number, and the value is the index in the desiredSortOrder array above
var mapToIndex = {};
desiredSortOrder.forEach((int, index) => mapToIndex[int] = index);
toSort.sort((a, b) => mapToIndex[a] - mapToIndex[b]);
console.log(toSort);
// [1, 1, 3, 3, 2, 2, 7, 7, 5, 5, 4, 4, 6, 6]

您可以使用Array#forEach()并使用新索引构建一个新数组。

var array = ['The Black Prince', 'The Sons of Hodir', 'Booty Bay', 'Zandalar Tribe', 'The Oracles'],
    sortOrder = [5, 3, 2, 1, 4];
function sort(array, sortOrder) {
    var result = [];
    sortOrder.forEach(function (a, i) {
        result[a - 1] = array[i];
    });
    return result;
}
document.write('<pre>' + JSON.stringify(sort(array, sortOrder), 0, 4) + '</pre>');

只是一个提示,您可以按行组织数据,其中一个项目的所有数据都收集在对象中,如以下表示:

[
    {
        "name": "The Black Prince",
        "standing": 3,
        "repstring": "Neutral",
        "value": 2800,
        "maxValue": 3000,
        "id": 1359,
        "sortOrder": 5
    },
    {
        "name": "The Sons of Hodir",
        "standing": 0,
        "repstring": "Hated",
        "value": 0,
        "maxValue": 36000,
        "id": 1119,
        "sortOrder": 3
    },
    // ...
]