0

I am trying to sort vote-list in PHP.

The list is an array, containing class-objects:

Array
(
    [0] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => bbh - bghs dsdw
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 6
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => bbh--0
                )

        )

    [1] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => aaa - bbb
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 4
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => aaa--0
                )

        )

    [2] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => wdewv - qwdqs
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 3
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => wdewv--0
                )

        )

    [3] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => Hsg and fdSv - aGamaama
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 2
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => hsgandfdsv--0
                )

        )
)

I managed to sort there by the ->key wich is working fine:

usort($votedsongs, function ($a, $b) { return $b->votes - $a->votes; });

But after this, I still need another sort-function to sort those songs that have the same amout of votes by ->title.

I already found some solutions that deal with problems alike, but those did not work for me.

Any ideas on this?

samtun
  • 434
  • 1
  • 3
  • 12
  • ` if (!($r = $b->votes - $a->votes)) $r = next_rules; return $r;` – splash58 Sep 03 '15 at 07:02
  • please explain a little, I don't know how to implement this. – samtun Sep 03 '15 at 07:06
  • 1
    `if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r;` – splash58 Sep 03 '15 at 07:07
  • so i replace my usort function like: `usort($votedsongs, function ($a, $b) { if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r; });` correct? Sadly this did not do the trick.. – samtun Sep 03 '15 at 07:14
  • change `title` to `titel` and what is result? – splash58 Sep 03 '15 at 07:16
  • wow okay that did something. Now It's sorted, but lowercase-titled songs come before uppercase-title ones now, like `z,t,m,a,Z,T,M,A`. Is there an easy way to merge these to one sort? – samtun Sep 03 '15 at 07:21
  • found a solution `strcasecmp()` thanks for that, I'm going to make this an answer. – samtun Sep 03 '15 at 07:36

2 Answers2

0

Sounds like you are wanting to sort the VotedSong objects in the array by votes and then by title (which is misspelled as titel). If so, this could work:

usort($votedsongs, function ($a, $b) {
    if ($b->votes == $a->votes) {
        return ($a->title < $b->title) ? -1 : 1;
    }
    return $b->votes - $a->votes;
});
Nerdwood
  • 3,947
  • 1
  • 21
  • 20
  • looks good, but it does not work, don't know why.. I got a solution already, thanks nevertheless. – samtun Sep 03 '15 at 07:40
0

Thanks to splash58 for this solution:

if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r;

I modified the alphabetic sort to be non-casesensitive and switched $a->title and $b->title - that's it:

usort($votedsongs, function ($a, $b) { 
    if (!($r = $b->votes - $a->votes)) $r = strcasecmp($b->title, $a->title); return $r; 
});
samtun
  • 434
  • 1
  • 3
  • 12