PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Mr Tech
Forum Contributor
Posts: 424 Joined: Tue Aug 10, 2004 3:08 am
Post
by Mr Tech » Sun Nov 25, 2007 9:17 pm
I've got the following code to sort an array:
Code: Select all
$files[1][file] = "1.jpg";
$files[1][file] = "2.jpg";
$files[1][file] = "3.jpg";
$files[1][file] = "4.jpg";
$files[1][file] = "5.jpg";
$files[1][file] = "6.jpg";
$files[1][file] = "7.jpg";
$files[1][file] = "8.jpg";
$files[1][file] = "9.jpg";
$files[1][file] = "10.jpg";
$files[1][file] = "11.jpg";
$files[1][file] = "12.jpg";
$files[1][file] = "13.jpg";
$files[1][file] = "14.jpg";
$files[1][file] = "15.jpg";
$files[1][file] = "16.jpg";
$files[1][file] = "17.jpg";
$files[1][file] = "18.jpg";
// etc etc etc
function cmp($a, $b) {
return strcmp($a["file"], $b["file"]);
}
usort($files, "cmp");
foreach($files as $id => $file) {
// php goes here...
}
This works however the 'files' orders like this:
1.jpg
10.jpg
11.jpg
etc
Instead of
1.jpg
2.jpg
3.jpg
etc
What am I doing wrong?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sun Nov 25, 2007 10:06 pm
how about:
Code: Select all
function cmp_int($a, $b) {
if ((int)$a["file"] == (int)$b["file"]) {
return 0;
}
return ((int)$a["file"] < (int)$b["file"]) ? -1 : 1;
}
(#10850)
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Sun Nov 25, 2007 10:19 pm
Have you had a look at
natsort() ?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.