mirror of
https://github.com/php/doc-es.git
synced 2026-03-24 07:22:16 +01:00
Use consistent line terminator. git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@317671 c90b9560-bf6c-de11-be94-00142212c4b1
248 lines
5.5 KiB
XML
248 lines
5.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 96c9d88bad9a7d7d44bfb7f26c226df7ee9ddf26 Maintainer: seros Status: ready -->
|
|
<refentry xml:id="function.usort" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>usort</refname>
|
|
<refpurpose>Ordena un array según sus valores usando una función de comparación definida por el usuario</refpurpose>
|
|
</refnamediv>
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>usort</methodname>
|
|
<methodparam><type>array</type><parameter role="reference">array</parameter></methodparam>
|
|
<methodparam><type>callback</type><parameter>cmp_function</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Esta función ordenará un array según sus valores usando una función de comparación
|
|
definida por el usuario. Si el array que se desea ordenar necesita ser ordenado por
|
|
algún criterio no trivial, debería usar esta función.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Si dos miembros se comparan como iguales, su orden en el array ordenado es indefinido.
|
|
</para>
|
|
</note>
|
|
¬e.no-key-association;
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>array</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
El array de entrada.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>cmp_function</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
La función de comparación debe devolver un valor de tipo integer menor que, igual,
|
|
o mayor que cero si el primer argumento se considera, respectivamente,
|
|
menor que, igual, o mayor que el segundo.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
&return.success;
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>4.1.0</entry>
|
|
<entry>
|
|
Se introdujo un nuevo algoritmo de ordenación. La función <parameter>cmp_function</parameter>
|
|
no mantiene el orden original de los elementos comparadados como iguales.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Ejemplo de <function>usort</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function cmp($a, $b)
|
|
{
|
|
if ($a == $b) {
|
|
return 0;
|
|
}
|
|
return ($a < $b) ? -1 : 1;
|
|
}
|
|
|
|
$a = array(3, 2, 5, 6, 1);
|
|
|
|
usort($a, "cmp");
|
|
|
|
foreach ($a as $clave => $valor) {
|
|
echo "$clave: $valor\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
0: 1
|
|
1: 2
|
|
2: 3
|
|
3: 5
|
|
4: 6
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Obviamente, en este caso tan trivial, la función <function>sort</function>
|
|
sería más apropiada.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Ejemplo de <function>usort</function> usando un array multidimensional
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function cmp($a, $b)
|
|
{
|
|
return strcmp($a["fruta"], $b["fruta"]);
|
|
}
|
|
|
|
$frutas[0]["fruta"] = "uvas";
|
|
$frutas[1]["fruta"] = "limones";
|
|
$frutas[2]["fruta"] = "manzanas";
|
|
|
|
usort($frutas, "cmp");
|
|
|
|
while (list($clave, $valor) = each($frutas)) {
|
|
echo "\$frutas[$clave]: " . $valor["fruta"] . "\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Cuando se ordena un array multidimensional, <varname>$a</varname> y
|
|
<varname>$b</varname> contienen referencias al primer índice del array.
|
|
</para>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
$frutas[0]: limones
|
|
$frutas[1]: manzanas
|
|
$frutas[2]: uvas
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Ejemplo de <function>usort</function> usando una función miembro de un objeto
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class ObjPrueba {
|
|
var $nombre;
|
|
|
|
function ObjPrueba($nombre)
|
|
{
|
|
$this->nombre = $nombre;
|
|
}
|
|
|
|
/* Ésta es la función de comparación estática: */
|
|
static function cmp_obj($a, $b)
|
|
{
|
|
$al = strtolower($a->nombre);
|
|
$bl = strtolower($b->nombre);
|
|
if ($al == $bl) {
|
|
return 0;
|
|
}
|
|
return ($al > $bl) ? +1 : -1;
|
|
}
|
|
}
|
|
|
|
$a[] = new ObjPrueba("c");
|
|
$a[] = new ObjPrueba("b");
|
|
$a[] = new ObjPrueba("d");
|
|
|
|
usort($a, array("ObjPrueba", "cmp_obj"));
|
|
|
|
foreach ($a as $elemento) {
|
|
echo $elemento->nombre . "\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
b
|
|
c
|
|
d
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>uasort</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
|
|
-->
|