简体   繁体   English

气泡按元素排序

[英]Bubble sort on elements

I have a table that contains the theoretical rate of something (id=ratebaseX) and the actual observed rate of something (id=rateseenX). 我有一张表,其中包含某些事物的理论速率(id = ratebaseX)和某些事物的实际观察速率(id = rateseenX)。 I have a javascript that runs through each pair and simple calculates the difference (id=calcX). 我有一个遍历每对的javascript,可以简单地计算出差异(id = calcX)。

After this difference is calculated, I would like to do a bubble sort on the calcIDs array based on this calculated difference. 计算完此差异后,我想根据此计算出的差异对calcIDs数组进行冒泡排序。 I don't actually want to change the html or anything thing in the calcIDs class, just the index in the array. 我实际上并不想更改calcIDs类中的html或任何东西,而只是更改数组中的索引。

For tests, I dump the first three array elements into an alert box because I know they are originally out of order. 对于测试,我将前三个数组元素转储到警报框中,因为我知道它们最初是乱序的。

function calcLikelihood(){
    ratebaseIDs = document.querySelectorAll('[id^="ratebase"]');
    rateseenIDs = document.querySelectorAll('[id^="rateseen"]');
    calcIDs = document.querySelectorAll('[id^="calc"]');

    for(i=0;i<calcIDs.length;i++){
        likelihood = Math.round((ratebaseIDs[i].innerHTML - rateseenIDs[i].innerHTML)*100)/100;
        calcIDs[i].innerHTML = likelihood;
        calcIDs[i].likelihood = likelihood;
    }
    //bubble sort based on likelihood
    for(i=0;i<calcIDs.length;i++){
        for(j=0;j<(calcIDs.length-1);j++){
            if(calcIDs[j].likelihood<calcIDs[j+1].likelihood){
                temp = calcIDs[j];
                calcIDs[j] = calcIDs[j+1];
                calcIDs[j+1] = temp;
            }
        }
    }
    alert("0) " + calcIDs[0].likelihood +"\n1) " + calcIDs[1].likelihood+"\n2) " + calcIDs[2].likelihood);
}

The problem is that first three elements do not move. 问题在于前三个元素不会移动。 I am pretty sure I got the bubble sorting correct. 我很确定我对气泡的排序是正确的。 I think the problem is that calcIDs is acutally an array of pointers? 我认为问题是calcIDs实际上是一个指针数组?

Your bubble sort is ok, I checked it: 您的气泡排序没问题,我检查了一下:

 var calcIDs = [{likelihood: 84}, {likelihood: 56}, {likelihood: 68}, {likelihood: 81}]; //bubble sort based on likelihood for(i=0;i<calcIDs.length;i++){ for(j=0;j<(calcIDs.length-1);j++){ if(calcIDs[j].likelihood<calcIDs[j+1].likelihood){ temp = calcIDs[j]; calcIDs[j] = calcIDs[j+1]; calcIDs[j+1] = temp; } } } console.log("0) " + calcIDs[0].likelihood +"\\n1) " + calcIDs[1].likelihood+"\\n2) " + calcIDs[2].likelihood); // 84, 81, 68 

So, I think the problem is that document.querySelectorAll doesn't return an array. 因此,我认为问题在于document.querySelectorAll不返回数组。 It returns an object of type NodeList whereas you can normally sort only true arrays in JS. 它返回类型为 NodeList的对象,而您通常只能在JS中对真实数组进行排序。 I mean you can apply a sorting algorithm to a collection, but the "sorted" items will be shown in the same predefined order as they have been shown before sorting. 我的意思是您可以对集合应用排序算法,但是“已排序”的项目将以与排序之前相同的预定义顺序显示。 A simple workaround might be converting the collection of nodes into an actual array like this: 一个简单的解决方法可能是将节点集合转换为实际的数组,如下所示:

var arr = Array.prototype.slice.call(nodes); // nodes = document.querySelectorAll(".your_selector");

and then you will be able to apply the sorting and get what you expect. 然后您将能够应用排序并获得期望的结果。 It is ok for your purpose because you state you don't want to change the view. 可以满足您的目的,因为您声明不想更改视图。 I hope this late answer helps :) 我希望这个较晚的答案有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM