8

I'm look to naturally sort an array, in reverse order and not preserve the keys. For example, I'd like this array:

[0] => 1-string
[1] => 2-string
[2] => 10-string
[3] => 4-srting
[4] => 3-srting

To end up like this:

[0] => 10-srting
[1] => 4-string
[2] => 3-string
[3] => 2-string
[4] => 1-string

I've got it close with usort($array, 'strnatcmp'); but it's not in reverse order. array_reverse() after doesn't help.

Any ideas?

2
  • Is that natural or unnatural ordering? I thought natural was "counting" so it would be 1-string, 10-string, 2-string, 3-string, 4-string and unnatural would be 4-string, 3-string, 2-string, 10-string, 1-string. I have been known to misunderstand things (as i am married and miscommunications happen 24/7), so could you give a more in depth explanation, for i do not understand your ordering system. Commented Mar 23, 2012 at 16:06
  • Natural is 1, 2, 3, 4, 10—which I can do. I now need to get that in reverse order, so: 10, 4, 3, 2, 1. Commented Mar 23, 2012 at 16:08

7 Answers 7

8

I'm a bit puzzled about "array_reverse() after doesn't help." because

<?php
echo PHP_VERSION, "\n";

$x = array( 
    '1-string',
    '2-string',
    '10-string',
    '4-srting',
    '3-srting'
);

natsort($x);
$x = array_reverse($x, false);
print_r($x);

prints

5.3.8
Array
(
    [0] => 10-string
    [1] => 4-srting
    [2] => 3-srting
    [3] => 2-string
    [4] => 1-string
)

on my machine

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

Comments

5

Use the $preserveKeys attribute of array_reverse() to reset the keys as well as reversing the array after a natcasesort().

function rnatcasesort(&$array) {
    natcasesort($array);
    $array = array_reverse($array, false);
}

$values = array('1-string', '2-string', '10-string', '4-string', '3-string');

rnatcasesort($values);

var_dump($values);

/*
array(5) {
  [0]=>
  string(9) "10-string"
  [1]=>
  string(8) "4-string"
  [2]=>
  string(8) "3-string"
  [3]=>
  string(8) "2-string"
  [4]=>
  string(8) "1-string"
}
*/

Comments

3

Use rsort() with the SORT_NATURAL flag.

rsort($array, SORT_NATURAL);

SORT_NATURAL was introduced in PHP 5.4. If you are on a lower version, go with the array_reverse(natsort()) version.

Comments

0

You could do

<?php
$arr  = array("1-string", "2-string", "10-string","4-srting", "3-srting");

function sort_reverse($a, $b){
  $a = (int)$a;
  $b = (int)$b;
  if ($a > $b){
    return -1;
  }
  if ($a < $b){
    return 1;
  }
   return 0;
}
usort($arr, "sort_reverse");
var_dump($arr);

pad here http://codepad.org/6dn81S3f

Comments

0

This works:

$array = array('1-string', '2-string', '10-string', '4-string', '3-string');
natsort($array);
$array = array_reverse($array);
print_r($array);

Comments

0

use this function to first do a natural sort and then by using array_reverse to make it in descending order

    function natrsort($array)
    {
      natsort($array);
      return array_reverse($array);
    }
    natrsort($array); 

Comments

-1

Doesn't rsort($array) fits your need?

1 Comment

Nope. That gets me 4, 3, 2, 10, 1.

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.