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
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 02-27-2010, 12:06 AM   #1 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default Perhaps I'm doing this wrong..

I've a multi-dimensional array containing real estate data such as price, year, etc. I'm attempting a bubble sort to order the data as desired ( Year Low to Year High, Price Low to Price High, etc ) and at first I tried simply using uasort but that failed and now I'm using the old fashioned for loop...yet...I still get it wrong, perhaps I'm simply implementing it right? o.O; I double-checked the algorithm on wikipedia and it looks right.

php Code:
<?php

/** Data for the purpose of testing **/
$demoData = 'a:6:{i:0;a:9:{s:7:"Address";s:14:"3630 NW 207 ST";s:8:"Bedrooms";s:1:"4";s:9:"BathsHalf";s:1:"0";s:9:"BathsFull";s:1:"2";s:4:"City";s:5:"Miami";s:10:"LivingArea";s:4:"1179";s:9:"ListingID";s:8:"M1317308";s:9:"ListPrice";s:6:"199000";s:5:"State";s:2:"FL";}i:1;a:9:{s:7:"Address";s:16:"630 SW 19 RD # B";s:8:"Bedrooms";s:1:"3";s:9:"BathsHalf";s:1:"0";s:9:"BathsFull";s:1:"2";s:4:"City";s:5:"Miami";s:10:"LivingArea";s:4:"2088";s:9:"ListingID";s:8:"M1345870";s:9:"ListPrice";s:6:"249382";s:5:"State";s:2:"FL";}i:2;a:9:{s:7:"Address";s:12:"13459 SW 118";s:8:"Bedrooms";s:1:"4";s:9:"BathsHalf";s:1:"1";s:9:"BathsFull";s:1:"3";s:4:"City";s:5:"Miami";s:10:"LivingArea";s:4:"2144";s:9:"ListingID";s:8:"M1352857";s:9:"ListPrice";s:6:"249900";s:5:"State";s:2:"FL";}i:3;a:10:{s:7:"Address";s:15:"4121 NW 21ST Av";s:8:"Bedrooms";s:1:"2";s:9:"BathsHalf";s:1:"0";s:9:"BathsFull";s:1:"1";s:4:"City";s:5:"Miami";s:10:"LivingArea";s:3:"941";s:17:"OriginalListPrice";s:6:"139000";s:9:"ListingID";s:8:"R3060687";s:9:"ListPrice";s:5:"99000";s:5:"State";s:2:"FL";}i:4;a:9:{s:7:"Address";s:13:"1053 SW 22 ST";s:8:"Bedrooms";s:1:"3";s:9:"BathsHalf";s:1:"0";s:9:"BathsFull";s:1:"3";s:4:"City";s:5:"Miami";s:10:"LivingArea";s:4:"1667";s:9:"ListingID";s:8:"M1329296";s:9:"ListPrice";s:6:"439000";s:5:"State";s:2:"FL";}i:5;a:9:{s:7:"Address";s:15:"10021 SW 108 ST";s:8:"Bedrooms";s:1:"4";s:9:"BathsHalf";s:1:"0";s:9:"BathsFull";s:1:"2";s:4:"City";s:5:"Miami";s:10:"LivingArea";s:4:"1977";s:9:"ListingID";s:8:"M1352823";s:9:"ListPrice";s:6:"374000";s:5:"State";s:2:"FL";}}';

$properties = unserialize($demoData);

// First, uasort.
uasort($properties, function($a, $b){
    return $a['ListPrice'] < $a['ListPrice'] ? -1 : 1;
});

// Tried this after uasort failed me
for($i = 1; $i < count($properties); ++$i)
{
    if( (int)$properties[$i]['ListPrice'] < (int)$properties[$i -1]['ListPrice'] )
    {
        $tA = $properties[$i -1];
        $tB = $properties[$i];
        $properties[$i -1] = $tB;
        $properties[$i]    = $tA;
    }
}

// wtf, man -.-
 
__________________
My Blog
Enfernikus is offline  
Reply With Quote
 



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
Anything wrong with this? shankar Absolute Beginners 2 04-17-2009 12:41 PM
What am I doing wrong here? Sam Granger General 2 04-16-2009 12:41 AM
whats wrong in this code ? [i am learning]! codefreek Absolute Beginners 24 07-03-2008 02:24 PM
Did I choose the wrong language? karq The Lounge 18 01-28-2008 05:04 AM


All times are GMT. The time now is 12:43 PM.

 
     

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