TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Perhaps I'm doing this wrong.. (http://www.talkphp.com/absolute-beginners/5288-perhaps-im-doing-wrong.html)

Enfernikus 02-27-2010 12:06 AM

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 -.-
 

delayedinsanity 02-27-2010 01:03 AM

You've pretty much got it, except that you're comparing $a to $a.

php Code:
header( 'Content-type: text/plain' );

$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);

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

print_r( $properties );

Enfernikus 02-27-2010 05:48 PM

...dammit.

Also, for the regular bubblesort algorithm, in case anyone else has this problem. There must be a secondary for loop Y where the parent for loop is X compare X vs Y and then shift positions.


All times are GMT. The time now is 02:04 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0