TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-25-2009, 10:24 AM   #1 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default Sorting an Array

Hey folks,

I have an array like this:
Code:
Array ( 
	[1] => Array ( 
		[team_name] => Steelers FC 
		[played] => 2 
		[points] => 4
		[won] => 1 
		[drawn] => 1 
		[lost] => 0 
		[for] => 5 
		[against] => 3 
		[goal_difference] => 2 
	) [2] => Array ( 
		[team_name] => Bishop Middleham 
		[played] => 2 
		[points] => 1 
		[won] => 0 
		[drawn] => 1 
		[lost] => 1 
		[for] => 3 
		[against] => 5 
		[goal_difference] => -2 
	) 
)
I need to sort it like a league table, so:

Points DESC,
For DESC,
Goal diff DESC

Anybody know how I would achieve this?

The final array might have 12/13/14 different entries in it rather than just the two shown.
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 08-25-2009, 02:10 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

This is really what databases are for, why do you have unsorted arrays this large?

If it must stay in an array, this algorithm is what I can give you off the top of my head (there are probably much more efficient ways of doing this):
Objective: To sort rows by points then for then goal:
1. Bubble sort by name
2. Split the array by name
3. Sort by for in each group
4. Split each of those arrays by for.
5. Sort by goals in each group.
6. Merge them back together.

Memory won't be an issue with only a few rows, but don't do this with hundreds of rows. This has potential to be a memory monster.
__________________

Village Idiot is offline  
Reply With Quote
Old 08-25-2009, 02:33 PM   #3 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

I didn't even think! The data has come from a database, and it should be sorted in the query - duh and thanks!
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 08-25-2009, 02:34 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

It happens, best of luck.
__________________

Village Idiot is offline  
Reply With Quote
Old 08-25-2009, 04:19 PM   #5 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

Ah, in fact that's no good come to think of it. I have a database which I retrieve data from, I then work on it (calculations and stuff) and store it in an array so I can easily pass it. Therefore I can't perform MySQL sorts on the data as the data is not ready to be sorted at that stage.

So, any idea's?
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 08-25-2009, 06:26 PM   #6 (permalink)
The Contributor
 
hello-world's Avatar
 
Join Date: Feb 2009
Posts: 73
Thanks: 30
hello-world is on a distinguished road
Default

I think you can you this function
PHP Code:
array_multisort() 
I read once in a book.
I see if could solve your next problem.

PHP Code:
$shop = array( array( 'team-name' => "rose"
                      
'played' => 64,
                      
'poinst' => 1,
                      
'won' => 6,
                      
'draw' => 4,
                      
'lost' => 2,
                      
'for' =>2,
                       
                    ),
               array(
'team-name' => "rose"
                      
'played' => 25,
                      
'poinst' => 5,
                      
'won' => 7,
                      
'draw' => 6,
                      
'lost' => 4,
                      
'for' =>2,
                    ),
               array(
'team-name' => "rose"
                      
'played' => 125,
                      
'poinst' => 15,
                      
'won' => 1,
                      
'draw' => 5,
                      
'lost' => 25,
                      
'for' =>2,
                    )
             );
             foreach(
$shop as $key => $value){
                 echo 
"<pre>";
                 echo 
$key; echo "=>";
                 foreach (
$value as $key => $value){
                     echo 
$key ."=>".$value;
                         echo 
"<br>";
                 }    
                 
             }
             
             echo 
"<hr>";

              foreach (
$shop as $key => $row) {
                
$won[$key]  = $row['lost'];
                
$draw[$key] = $row['draw'];
        }

   
array_multisort($won,$draw $shop);
     
    foreach(
$shop as $key => $value){
                 
                 echo 
$key; echo "=>";
                 foreach (
$value as $key => $value){
                     echo 
$key ."=>".$value;
                         echo 
"<br>";
                 }    
                 
             } 
hello-world is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Group Array "teams" and add up points godsdead General 3 07-15-2009 08:38 PM
How to search array benton General 3 03-29-2009 12:31 AM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
self submited form problem, sorting, cleaning and array... Peuplarchie Advanced PHP Programming 3 11-11-2008 12:08 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 07:08 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design