1

This is the array

$array = array(
   'list[P] = 1',
   'list[A] = 1',
   'list[F] = 2',
   'list[B] = 1'
);

This is the output I want

[0] => list[P] = 1
[1] => list[A] = 1
[2] => list[B] = 1
[3] => list[F] = 2

notice `list[P]` remains at the top because it is the first with value 1. So only numeric sorting.
1
  • Do you want "list" to be an array? Or is the "list[A]" a string? Commented Feb 20, 2011 at 9:16

3 Answers 3

5

Try taking a look at this documentation.

As you are trying to sort a specific part of the value, then you need to write your own comparator, and therefore the only method available is the usort function.

The uksort function takes two attributes, the array and the method name that you wish to use to do your comparison. You then write this method, which takes two values as a parameters, and return a true of false, depending on whether it is greater than or less than.

Therefore, you would have to substring the values coming in, to only compare the numbers.

The following code sample seems to work

function cmp($a, $b)
{
    $a = substr($a, -1);
    $b = substr($b, -1);
    return $a >= $b;
}

$array = array(
   'list[P] = 1',
   'list[A] = 1',
   'list[F] = 2',
   'list[B] = 1'
);

usort($array, "cmp");

var_dump($array);

The same code, only using PHP's anonymous function:

$array = array(
   'list[P] = 1',
   'list[A] = 1',
   'list[F] = 2',
   'list[B] = 1'
);

usort(
   $array,
   function ($a, $b){
      $a = substr($a, -1);
      $b = substr($b, -1);
      return $a >= $b;
   }
);

var_dump($array);
Sign up to request clarification or add additional context in comments.

10 Comments

@Codemwnci: Why am I not getting the above result?
@dany slightly misread your question, I have updated so it should now make sense!
Shouldn't that be usort because you are sorting by value? Anyway, the problem with usort (or uksort) is that if the comparison is equal, the elements are undefined...
Note: If two members compare as equal, their order in the sorted array is undefined. Apparently, this "note" is not clear enough... I tested and got an arranged array with no problems...
@Codemwnci: The alphabetical order is maintained until the number at list[F] is changed to 1. list[F] goes to the top of the list. Why is this?
|
0

If it is important to preserve records or keys with equal values in the same order they were input into the sort in the software you are writing then you would need a sort algorithm or method which is categorised as 'stable'. This limits the types of sort methods available to a programmer in any one country or a region within it in order to conform to the relevant rules.

Comments

0

To accommodate multi-digit numbers, parse the strings and capture the trailing number as an int-cast value.

Because array_multisort() will perform a final sort on the original input array, a stable sort is assured by sorting by original indexes after sorting by trailing number. Demo

array_multisort(
    array_map(
        fn($v) => sscanf($v, '%*s = %d')[0],
        $array
    ),
    array_keys($array),
    $array
);

Another way is to perform an explicit numeric sort by a sanitized copy of the array. Demo

array_multisort(
    preg_replace('#\D+#', '', $array),
    SORT_ASC,
    SORT_NUMERIC,
    array_keys($array),
    $array
);

With usort() you can directly enjoying a stable sort. The spaceship operator will automatically compare two numeric strings as numbers so no explicit casting is necessary. Demo

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

If you don't like the other people on your dev team, you can left trim a sufficiently wide range of characters to leave only numbers. Demo

usort(
    $array,
    fn($a, $b) => ltrim($a, ':..~') <=> ltrim($b, ':..~')
);

Or isolate substrings starting from the first encountered digit. Demo

usort(
    $array,
    fn($a, $b) => strpbrk($a, '0123456789') <=> strpbrk($b, '0123456789')
);

Comments

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.