treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Wordpress Sort Array Numerically

  • Hi Guys,

    If anyone can help me I would LOVE to hear it. I have a frustrated client and I am pulling my hair out and have been for days.

    We've set up drop down lists to allow people to filter posts based on custom post types.

    In order to populate the drop down list I am using:


    <select name="ratio" id="ratio">
    <?php
    $terms = get_terms("ratios");
    $count = count($terms);
    if ( $count > 0 ){
    foreach ( $terms as $term ) {
    echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
    }
    }?>
    </select>

    It outputs a long list of Ratio;s eg 1.67, 2.42, 10, 13.23 etc etc

    Listing these alphabetically doesn't work, as it outputs 1.67, 10, 2.42 etc

    The slug and the name are pretty similar, so I don't mind which one we are using.

    So I need to list them as integers... Any help would be really appreciated, whether its a php solutions, jQuery, wordpress... I don't care... I just need to make it happen.

    The site is here if you need more of an idea: http://amigaeng.com.au/gearbox-list/

    Thanks so much!

    Matt
  • Solved it with jQuery... I am definitely a jQuery hack and there are still a few strange issues going on... but its a lot better:


    $("#ratio").html($("#ratio option").sort(function (a, b) {
    var aValue = parseInt(a.value);
    var bValue = parseInt(b.value);
    return aValue == bValue ? 0 : aValue < bValue ? -1 : 1;
    }));