NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page [resolved] Sorting an array.

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 09-01-2007, 03:52 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Sorting an array.


I've got an array in this format:
PHP Code:
$array['text'] = number
????: NamePros.com http://www.namepros.com/programming/368875-resolved-sorting-an-array.html
How would I sort by the numbers, not the text? I've tried:
PHP Code:
$tmp = array();
foreach(
$array as $key => $var){
     
$tmp[$var] = $key;
}
$array ksort($tmp); 
But it dosn't work.
Barrucadu is offline  
Old 09-01-2007, 05:09 AM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
The following demonstrated how to do it. I have used print_r to show the order before and after the sorting.

PHP Code:
<?php
????: NamePros.com http://www.namepros.com/showthread.php?t=368875
$array 
= array('one'=>'1','three'=>'3','seven'=>'7','two'=>'2');
print_r($array);
asort($array);
print_r($array);
?>
The method of using ksort that you was using will not work in ordering the values in the array as ksort is used for ordering the key (where you have the text) ksort
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Last edited by peter@flexiwebhost; 09-01-2007 at 05:21 AM.
Peter is offline  
Old 09-01-2007, 06:14 AM THREAD STARTER               #3 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Nevermind, stupid mistake.
Last edited by Mikor; 09-01-2007 at 06:18 AM. Reason: Oops!
Barrucadu is offline  
Old 09-01-2007, 06:18 AM   #4 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
yeah I see what you have done. The asort returns 1 because it was successful in sorting the array.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 09-01-2007, 06:19 AM THREAD STARTER               #5 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Still not working
PHP Code:
echo "<pre>UNSORTED:\r\n";
????: NamePros.com http://www.namepros.com/showthread.php?t=368875
print_r($thing);
echo 
"\r\nSORTED:\r\n";
asort($thingSORT_NUMERIC);
print_r($thing);
echo 
"</pre>"
Outputs:
Code:
<pre>UNSORTED:
Array
(
    [yahoo] => 4
    [opera 9.22] => 9
    [internet explorer 7.0] => 12
    [internet explorer 6.0] => 29
)

SORTED:
Array
(
    [yahoo] => 4
    [opera 9.22] => 9
    [internet explorer 7.0] => 12
    [internet explorer 6.0] => 29
)
</pre>
Barrucadu is offline  
Old 09-01-2007, 06:25 AM   #6 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
can you post the full code and the expected output.

Looking at what you just posted I might be misunderstanding what you want. The array is already sorted by the values.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 09-01-2007, 06:26 AM   #7 (permalink)
mvl
fka: leonardo
Join Date: Aug 2006
Posts: 736
mvl is a glorious beacon of lightmvl is a glorious beacon of lightmvl is a glorious beacon of lightmvl is a glorious beacon of lightmvl is a glorious beacon of light
 



Originally Posted by Mikor
Still not working
PHP Code:
echo "<pre>UNSORTED:\r\n";
print_r($thing);
echo 
"\r\nSORTED:\r\n";
asort($thingSORT_NUMERIC);
print_r($thing);
echo 
"</pre>"
Outputs:
Code:
<pre>UNSORTED:
Array
(
    [yahoo] => 4
    [opera 9.22] => 9
    [internet explorer 7.0] => 12
    [internet explorer 6.0] => 29
)

SORTED:
Array
(
    [yahoo] => 4
    [opera 9.22] => 9
    [internet explorer 7.0] => 12
    [internet explorer 6.0] => 29
)
</pre>
The array looks sorted to me, although it already was sorted before the asort. How did expect it to look?
__________________
ForeignPropertyForSale.com
SATAN.EU VOI.EU
mvl is offline  
Old 09-01-2007, 06:46 AM THREAD STARTER               #8 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Got it working now, I needed to use arsort. (The fact that it was sorted before was a coincidence, it probably won't always be)
Barrucadu is offline  
Old 09-01-2007, 07:08 AM   #9 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
ahh so you wanted the numbers in reverse order i.e. highest first.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Closed Thread


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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 12:20 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger