Skip to content

Instantly share code, notes, and snippets.

@0xnbk
Created August 7, 2010 09:22
Show Gist options
  • Save 0xnbk/512622 to your computer and use it in GitHub Desktop.
Save 0xnbk/512622 to your computer and use it in GitHub Desktop.
natsort()
<?php
$items = array(
"100 apples", "5 apples", "110 apples", "55 apples"
);
// normal sorting:
sort($items);
print_r($items);
# Outputs:
# Array
# (
# [0] => 100 apples
# [1] => 110 apples
# [2] => 5 apples
# [3] => 55 apples
# )
natsort($items);
print_r($items);
# Outputs:
# Array
# (
# [2] => 5 apples
# [3] => 55 apples
# [0] => 100 apples
# [1] => 110 apples
# )
?>
@0xnbk
Copy link
Author

0xnbk commented Sep 13, 2010

natsort() is a function which will sort items in an array naturally (ie, in an order which seems logical to a person), rather than by characters’ ordinal values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment