私は自分のゲーム内のアイテムの目録を持っています。プレイヤーは名前、数量、タイプといういくつかの基準に基づいてアイテムを自動ソートする必要があります。私はアイテムの表のようにグリッドをソートする方法を見つけ出すように見えることはできませんしかしJavascriptでアイテムの2D配列/グリッド/テーブルを並べ替えます。
// create the Inventory grid
var InventoryWidth = 2;
var InventoryHeight = 4;
var Inventory = new Array(InventoryWidth);
for (var i = 0; i < InventoryWidth; i++) {
\t Inventory[i] = new Array(InventoryHeight);
}
// set the Items & default quantities
Inventory[0][0] = "Potion";
\t Inventory[1][0] = 2;
\t
Inventory[0][1] = "Elixir";
\t Inventory[1][1] = 9;
\t
Inventory[0][2] = "Antidote";
\t Inventory[1][2] = 5;
Inventory[0][3] = "Ether";
\t Inventory[1][3] = 1;
\t
// function for sorting items
function Sort2D(array2D, byColumn, ascending) {
\t // sort, seems I am using the wrong sorting function or my approach is wrong here:
// not sure how to do ASC/DESC as well
\t array2D.sort(function(a, b)
\t {
\t \t if(a[0] === b[0])
\t \t {
\t \t \t var x = a[byColumn].toLowerCase(), y = b[byColumn].toLowerCase();
\t \t \t
\t \t \t return x < y ? -1 : x > y ? 1 : 0;
\t \t }
\t \t return a[0] - b[0];
\t });
}
// sort all rows by first column: "name", setting to 1 should compare and sort the quantities instead
Sort2D(Inventory, 0, true);
// print grid contents
var output = "";
for(var i = 0; i < InventoryHeight; i++) {
\t
if (i == 0) {
output += " | name | own |";
}
\t for(var j = 0; j < InventoryWidth; j++) {
\t \t
\t \t if (j == 0) {
\t \t \t output += "\n"+i+"|";
\t \t }
\t \t
\t \t output+=Inventory[j][i];
\t \t
\t \t if (j >= Inventory[0].length-1) {
\t \t \t output += "|\n";
\t \t } else {
\t \t \t output += ", ";
\t \t }
\t }
}
console.log(output);
。 私はそれを選択された列とASC/DESCの順序でそれを持つ能力によって行の順序をソートする必要があります。私はこれについてどうやって行くのですか?
これはうまくいくようですが、このような配列構造を変更するとarray2dクラス全体が混乱するため、周囲のセルを反転させるようです。私は間違っていますか?これをリファクタリングすることは、この時点では大変な作業です。 あなたの方法で、私はそのようなアイテムを割り当てようとしました インベントリ[0、0] = "ポーション"; インベントリ[1、0] = 2; インベントリ[0、1] = "エリキシル"; インベントリ[1,1] = 9; インベントリ[0、2] = "解毒剤"; インベントリ[1、2] = 5; これはうまくいかなかったようですが、inventory.map関数は "inv"が何であるかわからないようでしたが、その関数の使い方はわかりません – user780756
リファクタリングをお勧めします。あなたのデータをそのままソートすることは可能ですが、より多くのステップがありますので、エラーが発生しやすくなります。 '.push()'、 '.splice()'や '.sort()'のような変更によって配列インデックスを失うことも非常に簡単です。あなたの配列にアイテムを割り当てることは、 'Inventory [0、1]'で行うことはできません。 'Inventory [0] [1]'が必要です。JavaScriptは大文字と小文字を区別しています。私の答えを見ると、 'Inventory'に小文字のIを付けました。おそらく' Inventory.map() 'はもっと成功するでしょう:) –