Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Custom Sorting (PHP code included)

    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:
    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:
    Code:
    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

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    not wuite sure what you are asking for.....


    do you just want to sort the array alphabetically?

    if so just do sizesArray.sort()
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I really didn't explain that correctly. I would like to take my current array:
    Code:
    var sizesArray =
    		[
    		    ["Large", "Large"]
    		  , ["ExtraLarge", "Extra Large"]
    		  , ["Small", "Small"]
    		  , ["Medium", "Medium"]
    		];
    And sort it as:
    Code:
    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)

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    Code:
    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"]

    ];
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #5
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That looks like it'll work pretty well. Thanks.

    EDIT: From the first couple tests, it looks like it runs prefectly. Thanks again!
    Last edited by Brandon MUS; 11-07-2006 at 12:31 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •