Jump to content


Photo

**SOLVED** Sorting several classes


  • Please log in to reply
3 replies to this topic

#1 frankvanlaere

frankvanlaere
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 May 2006 - 11:45 PM

Hello there,

I have a class (let's call it Mojo) which has values like id, position etc.
This class then gets initialized with:
Mojo austin1, austin2, austin3;

Now, how do I sort _all_ three classes on the value position? This value can change and at certain points they have to be sorted on that variable.

I've been trying stuff like sort(Mojo->position); but it doesn't seem to work. I could fix this by inserting them into a temporary table into a mysql database, and then re-fetching them in the correct order, but that's not very good is it? And I feel that there must be an easier solution.

Thanks!

ps. I know I haven't used the word classes correctly all the time, either way, there's only 1 class and when I say classes I mean those austin1, austin2, austin3 things, what do you call them? Instances?

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 27 May 2006 - 05:36 AM

[!--quoteo(post=377455:date=May 27 2006, 09:45 AM:name=TheScorpion)--][div class=\'quotetop\']QUOTE(TheScorpion @ May 27 2006, 09:45 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello there,

I have a class (let's call it Mojo) which has values like id, position etc.
This class then gets initialized with:
Mojo austin1, austin2, austin3;

Now, how do I sort _all_ three classes on the value position? This value can change and at certain points they have to be sorted on that variable.

I've been trying stuff like sort(Mojo->position); but it doesn't seem to work. I could fix this by inserting them into a temporary table into a mysql database, and then re-fetching them in the correct order, but that's not very good is it? And I feel that there must be an easier solution.

Thanks!

ps. I know I haven't used the word classes correctly all the time, either way, there's only 1 class and when I say classes I mean those austin1, austin2, austin3 things, what do you call them? Instances?
[/quote]


Best way is putting them in an array.
let say:
MojoArr = array();

MojoArr['austin1'] = new Mojo();
MojoArr['austin2'] = new Mojo();
MojoArr['austin3'] = new Mojo();

ksort(MojoArr); // will sort in accending order: austin1,2,3
krsort(MojoArr);// will sort in descending order: austin3,2,1

you got the idea.


Life's too short for arguing.

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 May 2006 - 09:37 AM

If you define the mojo class with position as the first class variable in the definition you can put them in an array and just sort the array.

Example
class mojo {
    var $position;
    var $id;
    
    function mojo ($id) {
        $this->id = $id;
        $this->position = rand(1,10);
    }
}

$austin = array();

for ($i=1; $i <= 3; $i++) {
    $austin[] = new mojo($i);
}

sort ($austin);

echo '<pre>', print_r($austin, true), '</pre>';


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 frankvanlaere

frankvanlaere
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 May 2006 - 04:17 AM

Thanks! :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users