Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/10545-solved-sorting-several-classes/
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.

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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.