pmerino
pmerino

Reputation: 6130

rsort() is putting strings with 10 after strings with 2

Well I have a PHP array like this:

Array 
( 
    [0] => post9.post 
    [3] => post8.post 
    [4] => post4.post 
    [5] => post7.post 
    [6] => post5.post 
    [7] => post10.post 
    [8] => post3.post 
    [9] => post6.post 
    [10] => post1.post 
    [11] => post2.post 
)

But after trying all the PHP array functions I don't manage to sort it in this way:

Array 
( 
    [0] => post10.post 
    [1] => post9.post 
    [2] => post8.post 
    [3] => post7.post 
    [4] => post6.post 
    [5] => post5.post 
    [6] => post4.post 
    [7] => post3.post 
    [8] => post2.post 
    [9] => post1.post 
)

I already made a question like this, which seemed to work but now it doesn't works :(

EDIT: After using rsortit looks like this:

Array
(
    [0] => post9.post
    [1] => post8.post
    [2] => post7.post
    [3] => post6.post
    [4] => post5.post
    [5] => post4.post
    [6] => post3.post
    [7] => post2.post
    [8] => post10.post
    [9] => post1.post
)

Almost good, except that post10.post should be [0] and is [8]

Upvotes: 1

Views: 58

Answers (1)

brian_d
brian_d

Reputation: 11385

You need to use natsort and then reverse the order.

 natsort($array);
 $array = array_reverse($array);

Upvotes: 7

Related Questions