1

Here is an example array I am attempting to sort:

$array = (object)array(

    'this' => 'that',
    'posts'=> array(
        'title' => '001 Chair',
        'title' => 'AC43 Table',
        'title' => '0440 Recliner',
        'title' => 'B419',
        'title' => 'C10 Chair',
        'title' => '320 Bed',
        'title' => '0114'
    ),
    'that' => 'this'
);

usort($array->posts, 'my_post_sort');

Here is the function I am using to sort:

function my_post_sort($a, $b) {

    $akey = $a->title;
    if (preg_match('/^[0-9]*$',$akey,$matches)) {
      $akey = sprintf('%010d ',$matches[0]) . $akey;
    }
    $bkey = $b->title;
    if (preg_match('/^[0-9]*$',$bkey,$matches)) {
      $bkey = sprintf('%010d ',$matches[0]) . $bkey;
    }

    if ($akey == $bkey) {
      return 0;
    }

    return ($akey > $bkey) ? -1 : 1;
}

This gives me the following results:

'posts', array(
    'title' => 'C10 Chair',
    'title' => 'B419',
    'title' => 'AC43 Table',
    'title' => '320 Bed',
    'title' => '0440 Recliner',
    'title' => '0114',
    'title' => '001 Chair'
)

Now, the last step I need is getting the numbers to appear (descending) before the letters (descending).

Here is my desired output:

'posts', array(
    'title' => '320 Bed',
    'title' => '0440 Recliner',
    'title' => '0114',
    'title' => '001 Chair',
    'title' => 'C10 Chair',
    'title' => 'B419',
    'title' => 'AC43'
)

I've tried all kinds of sorts, uasorts, preg_match, and other functions; and just cannot seem to figure out the last step.

Any suggestions or assistance? Thank you.

2 Answers 2

1

Try this comparing function:

function my_post_sort($a, $b) {

    $akey = $a->title;
    $bkey = $b->title;

    $diga = preg_match("/^[0-9]/", $akey);
    $digb = preg_match("/^[0-9]/", $bkey);

    if($diga && !$digb) {
        return -1;
    }

    if(!$diga && $digb) {
        return 1;
    }

    return -strcmp($akey, $bkey);
}

It will sort in descending order, but place digits before other symbols.

Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately, didn't work. I think the way the titles are structured is causing this to not work?? All titles can/probably will end with letters. Let me please adjust my question to reflect the additional array items.
I'm sorry, forgot to add "/" symbols in preg_match. Corrected.
Still not there. I'm trying to set up a php fiddle now. I still think it has to do with how titles are structured.
How did you get the url to the php lite fiddle? I have one, but I don't know how to "grab" the url to it? Thank you for your continued patience!
AHHH... got it got it :) You are an ALL-STAR! Thank you so much. Now I can reverse engineer your code and help me understand. Thank you so much.
|
0

First of all, I do not think your array can works... You can not have the same key many times on the same array level.

foreach ($array as $key => $title) {

        if ( is_numeric(substr($title, 0, 1)) ) {
            $new_array[$key] = $title;
        }
    }

    array_multisort($array, SORT_DESC, SORT_STRING);
    array_multisort($new_array, SORT_DESC, SORT_NUMERIC);
    $sorted_array = array_merge($array, $new_array);

6 Comments

Thank you. That works fine with same results as my function; and MUCH cleaner. However, the final array is still not sorted with numbers first. How would I move the numbers (descending) to appear before the letters (descending)? Thank you.
Sorry, you wanted desc, now I fix it.
Numbers always are less than letters, so in your case, this function would be enough.
The results are sorting like ('C10 chair', 'B419', ..., '0440 Recliner', '0014. I please need it like this ('0440 Recliner', '0014. ..., 'C10 chair', 'B419').
Yes, I messed up, letters are bigger... Try that. You must assign different key for each one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.