Jump to content

Archived

This topic is now archived and is closed to further replies.

frankvanlaere

**SOLVED** Sorting several classes

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[!--quoteo(post=377455:date=May 27 2006, 09:45 AM:name=TheScorpion)--][div class=\'quotetop\']QUOTE(TheScorpion @ May 27 2006, 09:45 AM) [snapback]377455[/snapback][/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.

Share this post


Link to post
Share on other sites
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
[code]
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>';[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.