sort

(PHP 4, PHP 5, PHP 7)

sort锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

说锟斤拷

sort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷颉5锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟斤拷榈ピ拷锟斤拷锟斤拷锟斤拷锟酵碉拷锟斤拷锟斤拷锟斤拷掳锟斤拷拧锟�

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

锟斤拷锟斤拷

array

要锟斤拷锟斤拷锟斤拷锟斤拷椤�

sort_flags

锟斤拷选锟侥第讹拷锟斤拷锟斤拷锟斤拷 sort_flags 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟侥憋拷锟斤拷锟斤拷锟斤拷锟轿拷锟�

锟斤拷锟斤拷锟斤拷锟酵憋拷牵锟�

  • SORT_REGULAR - 锟斤拷锟斤拷锟饺较碉拷元锟斤拷锟斤拷锟侥憋拷锟斤拷锟酵o拷
  • SORT_NUMERIC - 锟斤拷元锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟饺斤拷
  • SORT_STRING - 锟斤拷元锟斤拷锟斤拷为锟街凤拷锟斤拷锟斤拷锟饺斤拷
  • SORT_LOCALE_STRING - 锟斤拷锟捷碉拷前锟斤拷锟斤拷锟斤拷locale锟斤拷锟斤拷锟斤拷锟斤拷锟窖碉拷元锟斤拷锟斤拷锟街凤拷锟斤拷锟饺较o拷锟斤拷锟斤拷锟斤拷 setlocale() 锟斤拷锟侥变。
  • SORT_NATURAL - 锟斤拷 natsort() 锟斤拷锟狡讹拷每锟斤拷锟斤拷元锟斤拷"锟斤拷然锟斤拷顺锟斤拷"锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 PHP 5.4.0 锟斤拷锟斤拷锟斤拷锟侥★拷
  • SORT_FLAG_CASE - 锟杰癸拷锟斤拷 SORT_STRING 锟斤拷 SORT_NATURAL 锟较诧拷锟斤拷OR 位锟斤拷锟姐)锟斤拷锟斤拷锟斤拷锟街达拷小写锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟斤拷值

锟缴癸拷时锟斤拷锟斤拷 TRUE锟斤拷 锟斤拷锟斤拷锟斤拷失锟斤拷时锟斤拷锟斤拷 FALSE锟斤拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.4.0 锟斤拷锟斤拷锟� sort_flags 锟斤拷 SORT_NATURAL 锟斤拷 SORT_FLAG_CASE 锟斤拷支锟街★拷
5.0.2 锟斤拷锟斤拷锟� SORT_LOCALE_STRING锟斤拷

锟斤拷锟斤拷

Example #1 sort() 锟斤拷锟斤拷

<?php

$fruits 
= array("lemon""orange""banana""apple");
sort($fruits);
foreach (
$fruits as $key => $val) {
    echo 
"fruits[" $key "] = " $val "\n";
}

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

fruits 锟斤拷锟斤拷锟斤拷锟斤拷母顺锟斤拷锟斤拷锟斤拷

Example #2 使锟矫诧拷锟斤拷锟街达拷小写锟斤拷然锟斤拷锟斤拷锟� sort() 锟斤拷锟斤拷

<?php

$fruits 
= array(
    
"Orange1""orange2""Orange3""orange20"
);
sort($fruitsSORT_NATURAL SORT_FLAG_CASE);
foreach (
$fruits as $key => $val) {
    echo 
"fruits[" $key "] = " $val "\n";
}

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

fruits[0] = Orange1
fruits[1] = orange2
fruits[2] = Orange3
fruits[3] = orange20

fruits 锟斤拷锟斤拷锟斤拷锟� natcasesort() 锟侥斤拷锟斤拷锟�

注锟斤拷

Note: 锟剿猴拷锟斤拷为 array 锟叫碉拷元锟截革拷锟斤拷锟铰的硷拷锟斤拷锟斤拷锟解将删锟斤拷原锟叫的硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟角斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

Note: 锟酵达拷锟斤拷锟� PHP 锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷sort() 使锟斤拷锟斤拷 » Quicksort 实锟街的★拷 The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays. This is however an implementation detail you shouldn't rely on.

Warning

锟节对猴拷锟叫伙拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞币★拷模锟斤拷锟轿� sort() 锟斤拷锟杰伙拷锟斤拷锟斤拷锟斤拷锟皆ぶ拷慕锟斤拷锟斤拷

锟轿硷拷