1

I am wondering how to sort an array using bubblesort after that I parsed elements from a page using the DOM.

$doc = new DOMDocument;
$doc->validateOnParse = true;
//What url?
@$doc->loadHTML(file_get_contents('http://mylink.com/hi'));
$doc->saveHTML();
$doc->preserveWhiteSpace = false;
$dom_xpath = new DOMXpath($doc);
$spelers = $dom_xpath->query("//*[@class='clan__rowContainer']");

Next, I want to sort the elements in my "donatiesarray" from high to low using bubblesort:

    $array_size = count($donatiesArray) - 1;
for ($i = 0; $i < $array_size; $i++) {
    for ($j = 0; $j < $array_size; $j++) {
        if ($donatiesArray[$i] < $donatiesArray[$j]) {
            $temp = $donatiesArray[$i];
            $donatiesArray[$i] = $donatiesArray[$j];
            $donatiesArray[$j] = $temp;
        }
    }
}

However my output is:

Array ( [0] =>    0
[1] =>    102
[2] =>    104
[3] =>    10
[4] =>    114
[5] =>    116
[6] =>    126
[7] =>    136
[8] =>    138
[9] =>    150
[10] => 166
[11] => 16
[12] =>182
[13] =>186
[14] =>18
[15] =>218
[16] =>220
[17] =>224
[18] =>226
[19] =>248
[20] =>262
[21] =>262
[22] =>267
[23] =>268
[24] =>272
[25] =>282
[26] =>289
[27] =>310
[28] =>314
[29] =>317
[30] =>319
[31] =>340
[32] =>348
[33] =>404
[34] =>416
[35] =>42
[36] =>432
[37] =>448
[38] =>466
[39] =>46
[40] =>482
[41] =>502
[42] =>536
[43] =>66
[44] =>82
[45] =>92
[46] =>94
[47] =>162
) 

I have no idea how to sort it correctly. I tried other methods like usort. But without results.

John v B
  • 41
  • 1
  • 3
  • 1
    Possible duplicate of [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – rogerrw Apr 10 '18 at 17:35
  • Is there a reason that you want to use bubblesort? It has a complexity of O(n^2). PHP has a built in `usort` which will perform better – spectacularbob Apr 10 '18 at 17:56
  • @RogerWang I don't think so, this won't work. I have no idea how. – John v B Apr 10 '18 at 21:19
  • @spectacularbob There is no specific reason, but any other sorting function will result in the same array. – John v B Apr 10 '18 at 21:19

0 Answers0