mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-20 02:48:56 +00:00

Patch contributed by Sobak <msobaczewski@gmail.com>. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@350645 c90b9560-bf6c-de11-be94-00142212c4b1
205 lines
5.5 KiB
XML
205 lines
5.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<refentry xml:id="function.sort" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<refnamediv>
|
|
<refname>sort</refname>
|
|
<refpurpose>Sort an array</refpurpose>
|
|
</refnamediv>
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>sort</methodname>
|
|
<methodparam><type>array</type><parameter role="reference">array</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter><initializer>SORT_REGULAR</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
This function sorts an array. Elements will be arranged from
|
|
lowest to highest when this function has completed.
|
|
</para>
|
|
¬e.sort-unstable;
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>array</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
The input array.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>sort_flags</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
The optional second parameter <parameter>sort_flags</parameter>
|
|
may be used to modify the sorting behavior using these values:
|
|
</para>
|
|
<para>
|
|
Sorting type flags:
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara><constant>SORT_REGULAR</constant> - compare items normally;
|
|
the details are described in the <link linkend="language.operators.comparison">comparison operators</link> section</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><constant>SORT_NUMERIC</constant> - compare items numerically</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><constant>SORT_STRING</constant> - compare items as strings</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<constant>SORT_LOCALE_STRING</constant> - compare items as
|
|
strings, based on the current locale. It uses the locale,
|
|
which can be changed using <function>setlocale</function>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<constant>SORT_NATURAL</constant> - compare items as strings
|
|
using "natural ordering" like <function>natsort</function>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<constant>SORT_FLAG_CASE</constant> - can be combined
|
|
(bitwise OR) with
|
|
<constant>SORT_STRING</constant> or
|
|
<constant>SORT_NATURAL</constant> to sort strings case-insensitively
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
&return.success;
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>sort</function> example</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$fruits = array("lemon", "orange", "banana", "apple");
|
|
sort($fruits);
|
|
foreach ($fruits as $key => $val) {
|
|
echo "fruits[" . $key . "] = " . $val . "\n";
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
fruits[0] = apple
|
|
fruits[1] = banana
|
|
fruits[2] = lemon
|
|
fruits[3] = orange
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
The fruits have been sorted in alphabetical order.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>sort</function> example using case-insensitive natural
|
|
ordering</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$fruits = array(
|
|
"Orange1", "orange2", "Orange3", "orange20"
|
|
);
|
|
sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);
|
|
foreach ($fruits as $key => $val) {
|
|
echo "fruits[" . $key . "] = " . $val . "\n";
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
fruits[0] = Orange1
|
|
fruits[1] = orange2
|
|
fruits[2] = Orange3
|
|
fruits[3] = orange20
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
The fruits have been sorted like <function>natcasesort</function>.
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
¬e.no-key-association;
|
|
<note>
|
|
<simpara>
|
|
Like most PHP sorting functions, <function>sort</function> uses an
|
|
implementation of <link
|
|
xlink:href="&url.wiki.quicksort;">Quicksort</link>.
|
|
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.
|
|
</simpara>
|
|
</note>
|
|
<warning>
|
|
<simpara>
|
|
Be careful when sorting arrays with mixed types values because
|
|
<function>sort</function> can produce unexpected results,
|
|
if <parameter>sort_flags</parameter> is <constant>SORT_REGULAR</constant>,
|
|
</simpara>
|
|
</warning>
|
|
</refsect1>
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>asort</function></member>
|
|
<member><function>rsort</function></member>
|
|
<member>&seealso.array.sorting;</member>
|
|
</simplelist>
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|