...

View Full Version : Custom Sorting (PHP code included)



Brandon MUS
11-06-2006, 07:14 PM
I had some help in writing a PHP function that sorts an array of sizes in logical order. I was wondering if this would be easy to convert over to javascript as well. Here is the PHP code:

/*
* sort_by_size takes an Array of attributes and attempts to sort by size
* order
* @param &$productAttributesArray - an Array of attributes with ['prompt']
* and ['code'] fields
*/
function sort_by_size(&$productAttributesArray) {
//sort Attribute alphabetically first
asort($productAttributesArray);

//for each attribute, add a field for "size"
foreach($productAttributesArray as $id => $info) {
//compute size
$pieces = explode(' / ', $productAttributesArray[$id]['prompt']);
$productAttributesArray[$id]['size'] = trim(strtolower($pieces[0]));
}

//sort items using a custom comparison function
usort($productAttributesArray, "compare_by_size");

foreach($productAttributesArray as $id => $info) {
unset($productAttributesArray[$id]['size']);
}
}
/*
* compare_by_size will compare two arrays and return true if the size attribute
* of the first array is greater than that of the second. This function will
* also return false if there isn't a size attribute for the second array
* @param a - Array with an attribute of size
* @param b - Array with an attribute of size
* @return true or false
*/
function compare_by_size($a, $b) {
$order = array(
'small' => 1
, 'small (out of stock)' => 1
, 'medium' => 2
, 'medium (out of stock)' => 2
, 'large' => 3
, 'large (out of stock)' => 3
, 'extra large' => 4
, 'extra large (out of stock)' => 4
);
if (!isset($order[$b['size']]))
return false;
return ($order[$a['size']] > $order[$b['size']]) ? true : false;
}

My javascript Array looks more like this:

var sizesArray =
[
["Large", "Large"]
, ["ExtraLarge", "Extra Large"]
, ["Small", "Small"]
, ["Medium", "Medium"]
];

I'd appreciate any help on this. My JavaScript isn't what it used to be :D

brandonH
11-06-2006, 08:41 PM
not wuite sure what you are asking for.....


do you just want to sort the array alphabetically?

if so just do sizesArray.sort()

Brandon MUS
11-06-2006, 10:01 PM
Sorry, I really didn't explain that correctly. I would like to take my current array:

var sizesArray =
[
["Large", "Large"]
, ["ExtraLarge", "Extra Large"]
, ["Small", "Small"]
, ["Medium", "Medium"]
];

And sort it as:

var sizesArray =
[
["Small", "Small"]
, ["Medium", "Medium"]
, ["Large", "Large"]
, ["ExtraLarge", "Extra Large"]
];

There is also the possibility of Small (Out of Stock), Medium (Out of Stock), Large (Out of Stock), and Extra Large (Out of Stock). In those cases, the (Out of Stock) can just be ignored and sort the same way as above.

Also, if there are any values that are not SML or X, they should be sorted alphabetically (after any SML or X's)

brandonH
11-06-2006, 10:22 PM
var sizesArray =
[
["Large", "Large", "3"]
, ["ExtraLarge", "Extra Large", "4"]
, ["Small", "Small", "1"]
, ["Medium", "Medium", "2"]
];
i="0";
while(i<sizesArray.length){
sizesArray[i].sort();
i++
}


and if ther are anymore values to be added to the array, make sure that the third value is 5 or higher. it will sort all array values that have 5 in them alphabetically.

after sorting the above will look like this:

[
["1", "Small", "Small"]
,["2", "Medium", "Medium"]
,["3", "Large", "Large"]
,["4", "Extra Large", "ExtraLarge"]

];

Brandon MUS
11-07-2006, 12:19 AM
That looks like it'll work pretty well. Thanks.

EDIT: From the first couple tests, it looks like it runs prefectly. Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum