user1856596
user1856596

Reputation: 7233

Sorting array of strings by contained numbers?

I have those elements in an array:

/mypath/10_something_04_06.txt
/mypath/1_something_04_06.txt
/mypath/12_something_04_06.txt
/mypath/2_something_04_06.txt
/mypath/3_something_04_06.txt

Now I want to sort it, so that the numbers after /mypath/ are considered. This is what I tried:

sort($myArray, SORT_NUMERIC);
sort($myArray, SORT_STRING );

It doesn't work. Should be expected result like below:

/mypath/1_something_04_06.txt
/mypath/2_something_04_06.txt
/mypath/3_something_04_06.txt
/mypath/10_something_04_06.txt
/mypath/12_something_04_06.txt

Thanks!

Upvotes: 0

Views: 42

Answers (1)

schnawel007
schnawel007

Reputation: 4020

You are looking for natsort (Sort an array using a "natural order" algorithm)

Upvotes: 1

Related Questions