Jump to content

Recommended Posts

Sample array:

 

array('one','two','three','_zero');

 

How could I sort this array so that _zero moves to the front.. Any items that begin with an underscore should be moved to the front of the array to get something like so:

 

_zero, one, two, three

 

Thanks

You are going to have to write a custom sorter since there is no "logical" manner in which to sort the values. Those are string values, so PHP doesn't know how to interpret those strings back to their numeric values (which is apparently how you want them sorted).

 

Without knowing all the possible values you intend to use I can't provide a full solution. BUt, I will suggest you will want to use usort() or uasort() along with a custom function. If you provide more details on all the possible values and how they should be sorted I might be able to provide some code to get you started.

Well the possible values could be any string without an underscore in front.. There won't always be one with an underscore, but if there is I'd like to move it to the front some how.. The one with the underscore will always start with the underscore.. It will not be in the middle or at the end..

OK, so if you have two values with underscores, what is the logic to determine which one comes first? Here is a small function that will move values with underscores to the front, but there is no logic for putting them in any order at the front. Plus, without logic to sort the underscore values AND the other values, the current order will likely get messed up.

 

This will put the underscore values at the beginning but will mess up the order of the other values:

$testArray = array('one', '_two', 'two', '_three', 'three','_zero');

function sortUnderscoreToFront($a, $b)
{
    if(substr($a, 0, 1)=='_') { return -1; }
    if(substr($b, 0, 1)=='_') { return 1; }
    return 1;
}

usort($testArray, 'sortUnderscoreToFront');

print_r($testArray);

 

Output:

Array
(
    [0] => _zero
    [1] => _two
    [2] => _three
    [3] => one
    [4] => three
    [5] => two
)

 

If you need to maintain the order of the non-underscore values OR need the underscore values in a specific order it will require a more complex solution.

Well I did finally figure it out.. This seems to do the trick for me:

 

<?php
function sortUnderscoreToFront($a, $b) {
    if (substr($a, 0, 1) == '_') { return -1; }
    if (substr($b, 0, 1) == '_') { return 1; }
    return strcmp(strval($a), strval($b));
}
usort($testArray, 'sortUnderscoreToFront');
?>

 

It alphabetically sorts all the non underscore values alphabetically while still moving all the underscore values to the front.. It would still need some logic to alphabetically sort the underscore values if wanted..

Um, why didn't you SAY you wanted the non-underscore values sorted alphabetically. I stated that I assumed you wanted them sorted how they were originally so they would be in order to their numerical equivalence. If you want both the underscore and non-underscore values sorted alphabetically - that is easy.

 

function sortUnderscoreToFront($a, $b)
{
    if(substr($a, 0, 1)=='_' Xor substr($b, 0, 1)=='_')
    {
        //One value starts with an underscore and the other does not
        return (substr($a, 0, 1)=='_') ? -1 : 1;
    }
    //Both start with an underscore or both do not
    return strcmp(strval($a), strval($b));
}

I did end up saying that a couple posts ago, but I didn't initially say that because I didn't know what the solution was going to do to the array.. I started with a sort($array) and then ran this function to compare.. The sort ordered them and the function scrambled them again..

 

Anyways.. Now it works exactly perfect and we even got a few different ways to do the function..

 

Thanks a bunch mjdamato!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.