2
$array = array(
   'list1 - 1',
   'list7 - 1',
   'list5 - 2',
   'list3 - 1'
);

I need sort to this array numerically. The function should check the value after the equal sign for sorting.


result:
list1 - 1
list7 - 1
list3 - 1
list5 - 2

In the above result, the order is not changed either alphabetically or numerically until `list[5]`. How do I do this?
2
  • Do you mean you want to sort it by key? (something like list1 -1, list3-1, list5-2, list7-1) Commented Feb 20, 2011 at 16:09
  • @Damien Pirsy: No not by key, I want to move list5 at the bottom of the list because it contains the number 2. Commented Feb 20, 2011 at 16:27

5 Answers 5

4

create a custom sort and execute through the usort function

function mysort($a,$b)
{
    $a = substr($a,-1,1);
    $b = substr($b,-1,1);

    if ($a == $b)
    {
        return 0;
    }

    return ($a < $b) ? -1 : 1;
}

and then pass it into usort with your array like so:

 usort($myarray, "myarray");

--

You request is very hard to accomplish as you want to sort an array with no logical identifier to work with, our minds interpret your result as possible but without creating a matrix, a hard coded concept its hard to accomplish, you best bet is if the class is always exactly like the above then manually do this:

$temp = $array[3];
$array[3] = $array[4];
$array[4] = $temp;

Ref: http://www.php.net/manual/en/function.usort.php

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

6 Comments

You should name this comparing function better mycompare, or even better compare_by_last_byte.
the OP may have a different naming convention to me so i kept it plain and simple.
@RobertPitt: if you look at the array, you see that list1 is first and list7 is second and list3 is the third. I want it in this order.
that would have to be done manually, as there is nothing to pro-grammatically distinguish the order
@RobertPitt: yes there is, just got figure it out
|
3

It looks like you want to search firstly on the trailing numbers, but if they are the same then you want to retain the original order of the items. This means a little more thinking is necessary but the job is, in essence, still very simple.

$array = array(
   'list1 - 1',
   'list7 - 1',
   'list5 - 2',
   'list3 - 1',
);

function sort_list(&$subject) {
    // Get an array of the trailing numbers to use within the callback
    $nums = preg_replace('/^.* - (\d+)$/', '$1', $subject);
    // Sort values by trailing number or key
    uksort($subject, function($a,$b) use ($nums) {
        // If numbers are the same, sort by key
        if ($nums[$a] === $nums[$b]) {
            return $a - $b;
        }
        // Othwerise sort by numbers
        return $nums[$a] - $nums[$b];
    });
}

sort_list($array);
var_dump($array);

Outputs something like:

array(4) {
  [0]=>
  string(9) "list1 - 1"
  [1]=>
  string(9) "list7 - 1"
  [3]=>
  string(9) "list3 - 1"
  [2]=>
  string(9) "list5 - 2"
}

Comments

1

Where $array is the name of your array. This function works if the numbers are greater than 1 digit.

function custom_sort($a, $b)
{
   $exp = '/(\d+)$/';
   preg_match($exp, $a, $a);
   preg_match($exp, $b, $b);
   return intval($a[1]) - intval($b[1]);
}

usort($array, 'custom_sort');

Comments

0

PHP assures stable sorting as of PHP8, so using explode() calls inside of usort() will allow the isolation of the trailing numbers and the spaceship operator will evaluate the numbers as integer values. Demo

usort(
    $array,
    fn($a, $b) => explode(' - ', $a)[1]
                  <=>
                  explode(' - ', $b)[1]
);
var_export($array);

Comments

-1
function mySort($array) {
    foreach ($array as $key => $value)  {  
        $array[$key] = explode(" - ", $array[$key]);
        $data[$value] = $array[$key][1];
    }
    asort($data);
}

1 Comment

list3 goes to the top of the list, i need it move to the bottom -- as in after list7 and before list5. see my question above. list1 must stay the first

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.