1

I have a data like this

array([0] => stdClass Object ( [id] => 1 [title] => AAAA [sentence] => abcdefgh [rank] => 3 ) [1] => stdClass Object ( [id] => 2 [title] => BBBB [sentence] => ijklmn [rank] => 1 ) [3] => stdClass Object ( [id] => 3 [title] => CCC [sentence] => opqrstu [rank] => 2 ));

Show the data:

foreach($data as $d)
{
   $title = $d->title;
   $sentence = $d->sentence;
   $rank = $d->rank;
   echo $title. " | " .$sentence. " | " .$rank. "<br>";
}

How to sort it by 'rank'? Thanks for your help.

bob
  • 445
  • 5
  • 8
  • 15

3 Answers3

0

use php's usort function

usort($array, function($a, $b){
    if ($a->rank == $b->rank) return 0;
    return ($a->rank > $b->rank)?1:-1;
});

usort takes the array and a comparison function, the comparison function should return 0 if the two parameters are equal in terms of the sort, 1 if the first argument is greater then the second argument and -1 if the first argument is less then the second.

Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • 1
    Just to clarify slightly: the callback doesn't need to return 1 or -1, only an integer greater than or less than 0. This means that you can simplify the callback in this case (where the field being compared is an integer) to `function($a, $b) { return $a->rank - $b->rank; }` – IMSoP May 09 '13 at 13:14
  • @IMSop elegant...I'll have to remember that. – Orangepill May 09 '13 at 14:39
0

You can use PHP's usort with a function that uses objects' attribute rank as criteria:

function rank_compare($a, $b) {
    return $a->rank - $b->rank;
}

usort($data, 'rank_compare');

More info: http://www.php.net/manual/en/function.usort.php

Phellipe Ribeiro
  • 491
  • 3
  • 13
  • with `return strcmp($a->rank, $b->rank);`, I can sholved this problem. Thanks for your help. [link](http://stackoverflow.com/questions/4282413/php-sort-array-of-objects-by-object-fields) – bob May 09 '13 at 14:22
  • You're welcome @bob! Once your `rank` is actually a number I think a numerical operator would be more suitable. – Phellipe Ribeiro May 09 '13 at 14:31
-1

This is where your classes on bubble sorting come in handy. You write yourself a handy little sorting algorithm that takes in an array as an argument, returns an array of sorted stuff... and it's just a couple of for loops. Outer loop is from 0 to the length of the array. Inner loop is from 0 to the length of the array minus the outer loop. And for the inner loop, compare current value to next value - if current value is 'bigger', bubble it up. After the first time through the outer loop, the biggest value is at the top. After the second time through, the second biggest is second from the top. Etc.

Wolfman Joe
  • 799
  • 1
  • 8
  • 23
  • Bubblesort in production code??!?!?! – Jon May 09 '13 at 12:58
  • Hey, if he doesn't know how to do a sort, he's gotta start somewhere. Admittedly, I'd much rather keep it sorted and indexed in the first place... – Wolfman Joe May 09 '13 at 13:02
  • I agree. And that somewhere is the manual page for the built-in function `sort`. – Jon May 09 '13 at 13:04
  • If you don't understand basic sorting algorithms, you won't use sort properly. I've worked with way too much bad code by people who just jumped into using sort without knowing what a sorting algorithm actually was. – Wolfman Joe May 09 '13 at 13:07
  • I'm with you, @WolfmanJoe ... PHP is ruined by those so-called developers which doesn't understand a damned thing in Computer Science, just grab CodeIgniter and start bashing away on the keyboard. – STT LCU May 09 '13 at 13:12
  • 2
    If the question was about sorting algorithm theory then you would have a point (but the answer would still be bad because bubble is about the worst sort you can do, and you do not even mention any alternative). As it stands, the question is "how do I sort this array?" and "write your own bubblesort" is a very bad answer. – Jon May 09 '13 at 13:14
  • in my book "use this" < "learn this". the poster is a noob so some basic theory would help more than a link to usort – STT LCU May 09 '13 at 13:15
  • @STTLCU: Feel free to point that out in a comment (but try to leave the "noob" part out). If you choose to **answer**, then **answer the question**. – Jon May 09 '13 at 13:16
  • @Jon Yes. And that's why I gave him a link to a page that says, "This is about the worst sort you can do, here are the alternatives." – Wolfman Joe May 09 '13 at 13:17
  • Fair enough, we both have explained our positions, I don't think arguing more is constructive. – Jon May 09 '13 at 13:18