Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript sort()返回的true/false与预期不同_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript sort()返回的true/false与预期不同

Javascript sort()返回的true/false与预期不同,javascript,arrays,sorting,Javascript,Arrays,Sorting,在下面的代码示例中,我将“item”作为石头或铜传递。这意味着要获取所有库存项目,按字母顺序排序,并再次追加覆盖旧项目的内容。我的问题是第一次,排序函数将“a”计算为铜质,将“b”计算为石头,并返回-1(false,correct?)。然后,如果一个值为0,我就得到了它,如果它删除了元素。当把这个元素加回去时,它的“a”是石头,“b”是铜,再次返回-1,然后首先用石头对它进行排序。我不知道怎么/为什么,我想我已经盯着这个太久了,我看不出有什么不对。任何帮助都将不胜感激!最小可复制示例: var

在下面的代码示例中,我将“item”作为石头或铜传递。这意味着要获取所有库存项目,按字母顺序排序,并再次追加覆盖旧项目的内容。我的问题是第一次,排序函数将“a”计算为铜质,将“b”计算为石头,并返回-1(false,correct?)。然后,如果一个值为0,我就得到了它,如果它删除了元素。当把这个元素加回去时,它的“a”是石头,“b”是铜,再次返回-1,然后首先用石头对它进行排序。我不知道怎么/为什么,我想我已经盯着这个太久了,我看不出有什么不对。任何帮助都将不胜感激!最小可复制示例:

var ConsoleButtons = {
    initialize: function() {
    cbOne.addEventListener("click", function() { ConsoleButtons.cbOneClick(); });
    cbOneClick: function() {
        PlayerInventory.addToBag(stone, 1);
        PlayerInventory.addToBag(copper, 1);
    }  
    }
}
var PlayerInventory = {
    initialize: function() {
    plyrInv = document.createElement('div');
    plyrInv.setAttribute('id', "playerInv");
    wrapper.append(plyrInv);
    },
    addToBag: function(item, qty) {
        item.quantity += qty;
        console.log(item.name + item.quantity);
        PlayerInventory.checkInv(item);
    },
    removeFromBag: function(item, qty) {
        item.quantity -= qty;
        if (item.quantity < 0) {
            item.quantity = 0;
        }
        console.log(item.name + item.quantity);
        PlayerInventory.checkInv(item);
    },
    createRescDisp: function(item) {
        if (item.para === undefined || item.para === null) {
            item.para = document.createElement('p');
            item.para.setAttribute('class', 'inventoryItem');
            item.para.setAttribute('id', item);
            item.para.innerHTML = item.name + ": " + item.quantity;
            invParent = document.getElementById('playerInv');
            invParent.appendChild(item.para);
            //Sort: compare a to b.
            //If a is larger than b, return true. else, false.
            invParArr = Array.from(invParent.children);
            //invParArr.sort((a,b) => a.getAttribute('id') > b.getAttribute('id') ? 1 : -1);
            for (i = 0; i < invParArr.length; i++) {
                invParent.append(invParArr[i]);
            }
}
PlayerInventory.initialize();
ConsoleButtons.initialize();
var控制台按钮={
初始化:函数(){
addEventListener(“单击”,函数(){ConsoleButtons.cbOneClick();});
cbOneClick:function(){
PlayerInventory.addToBag(石头,1);
PlayerInventory.addToBag(铜,1);
}  
}
}
变量PlayerInventory={
初始化:函数(){
plyrInv=document.createElement('div');
plyrInv.setAttribute('id','playerInv');
append(plyrInv);
},
addToBag:功能(项目、数量){
项目数量+=数量;
console.log(item.name+item.quantity);
PlayerInventory.checkInv(项目);
},
从袋子中取出:功能(项目、数量){
item.quantity-=数量;
如果(项目数量<0){
项目数量=0;
}
console.log(item.name+item.quantity);
PlayerInventory.checkInv(项目);
},
createRescDisp:函数(项){
如果(item.para==未定义| | item.para==空){
item.para=document.createElement('p');
item.para.setAttribute('class','inventoryItem');
item.para.setAttribute('id',item);
item.para.innerHTML=item.name+“:”+item.quantity;
invParent=document.getElementById('playerInv');
invParent.appendChild(第段项);
//排序:比较a和b。
//如果a大于b,则返回true,否则返回false。
invParArr=Array.from(invParent.children);
//invParArr.sort((a,b)=>a.getAttribute('id')>b.getAttribute('id')?1:-1);
对于(i=0;i
“并返回-1(false,correct?”“不,它表示在
a
之后排序
b
”@VLAZ或者,它[
-1
]表示
a
b
下面“下沉”,而返回
+1
则使其“浮动”上面的
b
。请注意,排序比较器需要返回三个值:小于零、大于零和零。缺少最后一个值,因此无法处理元素之间的相等。有关如何处理
.sort()的详细信息,请参阅
使用比较器的结果。请包含一个。几乎不需要。您只需要排序的项目列表。然后删除一个项目,重新添加,然后再次排序。不需要任何其他内容,如创建HTML、管理库存等的代码。。。