View Single Post
Old 02-27-2010, 12:06 AM   #1 (permalink)
Enfernikus
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