0

My array stores these variables:$arrgames = [$gametitle, $description, $releaseDate $score, $image] I have made the array from an object and am trying to sort them in highest to lowest $score. Through research this is the furthest I could get to making the bubble sort:

$tgameslist = DAL_CreateGames(); 
$arrgames = [];

foreach ($tgameslist->gameitems as $tgitem) {
    array_push($arrgames, $tgitem);
}
$size = count($arrgames) - 1;

for ($i = 0; $i > 3; $i ++) {
    for ($j = 0; $j < $size; $j ++) {
        $position = $j + 1;
        if ($arrgames[$position] < $arrgames[$j]) {
            list ($arrgames[$j], $arrgames[$position]) = array(
                $arrgames[$position],
                $arrgames[$j]
            );
        }
    }
}

But with the code above I don't know how to pretty much tell it which part of the array needs to be compared(score)

What I am essentially trying to do is

$tgamedata[] = new BLLGameItem(3, "Fifa 20", "blah blah", "Sep 24, 2019", "79", "fifa20.jpg");
$tgamedata[] = new BLLGameItem(1, "GTA V", "blah blah", "Nov 18, 2014", "97", "gtaV.jfif");

Sort these by their score ("79" and "97") in a function and return them sorted

mikaelwallgren
  • 170
  • 1
  • 9
C29
  • 3
  • 2

2 Answers2

0

Is there any particular reason why you want to use bubble sorting? This simple algorithm performs poorly in real world use and is used primarily as an educational tool. Read more at Wikipedia - Bubble sorting

I would recommend using usort instead.

Here is an example using usort, assuming the object BLLGameItem has a member named rating:

function sortByRatingDesc($a, $b) {
    return $b->rating - $a->rating;
}

$tgamedata[] = new BLLGameItem(3, "Fifa 20", "blah blah", "Sep 24, 2019", "79", "fifa20.jpg");
$tgamedata[] = new BLLGameItem(1, "GTA V", "blah blah", "Nov 18, 2014", "97", "gtaV.jfif");

usort($tgamedata, 'sortByRatingDesc');

You can test the example code here

However, if you really want to use bubble sort, here is an example of that (still assuming the object BLLGameItem has a member named rating):

function bubbleSort($array){
    $swapped = true;
    while($swapped){
        $swapped = false;
        for($i = 0, $c = count($array) - 1; $i < $c; $i++){
            if($array[$i]->rating < $array[$i + 1]->rating){
                list($array[$i + 1], $array[$i] ) = array($array[$i], $array[$i + 1]);
                $swapped = true;
            }
        }
    }
    return $array;
}

$tgamedata = bubbleSort($tgamedata);

You can test the example code here

mikaelwallgren
  • 170
  • 1
  • 9
  • That's a great answer thank you and no I was thinking about it all wrong and have now found a way to implement the sort using usort but thanks for your answer – C29 Apr 30 '20 at 10:09
0

I have now found a solution to my problem but thanks for the answers:

 function comparator($object1, $object2)
{
    return $object1->score < $object2->score;
}

// Use usort and the comparator function to sort the array
usort($tgamedata, 'comparator');

// Print to test

$tgame = new BLLGamesList();
$tgame->gameitems = $tgamedata;
return $tgame;

}

C29
  • 3
  • 2