Sorting an array of classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theWondering
Forum Newbie
Posts: 2
Joined: Thu Apr 08, 2004 12:08 pm

Sorting an array of classes

Post by theWondering »

Hi there!

I have a class:

Code: Select all

class Player
	{
		var $name;
		var $kills = 0;
		var $deaths = 0;
		var $rank = 0;
		var $weapon = array("THOMPSON"=>0,"MP40"=>0,""=>0,"DYNAMITE"=>0,"GRENADE_LAUNCHER"=>0,
							"BROWNING"=>0,"MACHINEGUN"=>0,"FLAMETHROWER"=>0,"PANZERFAUST"=>0,
							"FG42"=>0,"FG42SCOPE"=>0,"COLT"=>0,"LUGER"=>0,"LANDMINE"=>0,"ARTY"=>0,
							"MORTAR"=>0,"M7"=>0,"CARBINE"=>0,"AIRSTRIKE"=>0,"STEN"=>0,"K43"=>0,
							"AKIMBO_COLT"=>0,"AKIMBO_LUGER"=>0,"MG42"=>0,"MOBILE_MG42"=>0,
							"KAR98"=>0,"KNIFE"=>0,"GPG40"=>0,"SATCHEL"=>0,"CRUSH_CONSTRUCTION"=>0,
							"GARAND"=>0,"GARAND_SCOPE"=>0,"SILENCER"=>0,"K43_SCOPE"=>0,
							"AKIMBO_SILENCEDLUGER"=>0,"SILENCED_COLT"=>0,"AKIMBO_SILENCEDCOLT"=>0);

		function dominant_weapon()
		{
			$res = 0;
			foreach($this->weapon as $tot)
			{
				if($tot > $res)
				{
					$res = $tot;
				}
			}
			return array_search($res,$this->weapon);
		}
	}
Now, during the parsing of my file, I create an arry of this class in a loop thats like so:

Code: Select all

$plї$player_count] = new Player;
					$plї$player_count]->name = $matchї1];

					$plї$player_count]->kills += 1;
					$player_count += 1;
Now, what I would like to do is SORT this $pl array by ->kills How the HECK would I do something like this? I have tried everything under the sun :-(

Thanks
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

I don't think you will be able to use any built in sort thing you'll have to do your own!
theWondering
Forum Newbie
Posts: 2
Joined: Thu Apr 08, 2004 12:08 pm

Yeah

Post by theWondering »

Thats what Im thinking, but how?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Wait, are you pulling these from a DB, look up ORDERBY !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Wow, PHP amazes me again! What didn't they think of?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd like to see labels and goto statements myself.
Post Reply