Files
godot-docs-l10n/classes/es/class_array.rst
Rémi Verschelde cf78697eea Add localized class reference as pre-generated RST files
Currently including `zh_CN` and `es` which both have very high completion
ratios. Others will be added once they reach a significant percentage too.

These RST files will be used by godot-docs in place of its `classes` folder
after we sync with https://github.com/godotengine/godot-docs/pull/5458.

The update workflow is manual for now (example for `zh_CN`):

- Build `godotengine/godot` in the branch we currently track (now `3.x`)
- Run `godot --doctool -l zh_CN`
- Run `cd doc && make rst LANGARG=zh_CN`
- Copy `doc/_build/rst/*` to `classes/zh_CN/` here
- Make sure to have `classes/zh_CN/index.rst` copied from `docs/classes`
2021-12-21 16:07:55 +01:00

530 lines
35 KiB
ReStructuredText

:github_url: hide
.. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
.. DO NOT EDIT THIS FILE, but the Array.xml source instead.
.. The source is found in doc/classes or modules/<name>/doc_classes.
.. _class_Array:
Array
=====
A generic array datatype.
Descripción
----------------------
A generic array that can contain several elements of any type, accessible by a numerical index starting at 0. Negative indices can be used to count from the back, like in Python (-1 is the last element, -2 is the second to last, etc.).
\ **Example:**\
::
var array = ["One", 2, 3, "Four"]
print(array[0]) # One.
print(array[2]) # 3.
print(array[-1]) # Four.
array[2] = "Three"
print(array[-2]) # Three.
Arrays can be concatenated using the ``+`` operator:
::
var array1 = ["One", 2]
var array2 = [3, "Four"]
print(array1 + array2) # ["One", 2, 3, "Four"]
\ **Note:** Concatenating with the ``+=`` operator will create a new array, which has a cost. If you want to append another array to an existing array, :ref:`append_array<class_Array_method_append_array>` is more efficient.
\ **Note:** Arrays are always passed by reference. To get a copy of an array that can be modified independently of the original array, use :ref:`duplicate<class_Array_method_duplicate>`.
\ **Note:** When declaring an array with ``const``, the array itself can still be mutated by defining the values at individual indices or pushing/removing elements. Using ``const`` will only prevent assigning the constant with another value after it was initialized.
Métodos
--------------
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolColorArray<class_PoolColorArray>` from **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolVector3Array<class_PoolVector3Array>` from **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolVector2Array<class_PoolVector2Array>` from **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolStringArray<class_PoolStringArray>` from **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolRealArray<class_PoolRealArray>` from **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolIntArray<class_PoolIntArray>` from **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolByteArray<class_PoolByteArray>` from **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`append<class_Array_method_append>` **(** :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`append_array<class_Array_method_append_array>` **(** :ref:`Array<class_Array>` array **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`back<class_Array_method_back>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`bsearch<class_Array_method_bsearch>` **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`bsearch_custom<class_Array_method_bsearch_custom>` **(** :ref:`Variant<class_Variant>` value, :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func, :ref:`bool<class_bool>` before=true **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`clear<class_Array_method_clear>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`count<class_Array_method_count>` **(** :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`duplicate<class_Array_method_duplicate>` **(** :ref:`bool<class_bool>` deep=false **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`empty<class_Array_method_empty>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`erase<class_Array_method_erase>` **(** :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`find<class_Array_method_find>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`find_last<class_Array_method_find_last>` **(** :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`front<class_Array_method_front>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`has<class_Array_method_has>` **(** :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`hash<class_Array_method_hash>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`insert<class_Array_method_insert>` **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`invert<class_Array_method_invert>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`max<class_Array_method_max>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`min<class_Array_method_min>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`pop_at<class_Array_method_pop_at>` **(** :ref:`int<class_int>` position **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`pop_back<class_Array_method_pop_back>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`pop_front<class_Array_method_pop_front>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`push_back<class_Array_method_push_back>` **(** :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`push_front<class_Array_method_push_front>` **(** :ref:`Variant<class_Variant>` value **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`remove<class_Array_method_remove>` **(** :ref:`int<class_int>` position **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`resize<class_Array_method_resize>` **(** :ref:`int<class_int>` size **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`rfind<class_Array_method_rfind>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`shuffle<class_Array_method_shuffle>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`size<class_Array_method_size>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`slice<class_Array_method_slice>` **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`sort<class_Array_method_sort>` **(** **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`sort_custom<class_Array_method_sort_custom>` **(** :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func **)** |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Descripciones de Métodos
------------------------------------------------
.. _class_Array_method_Array:
- :ref:`Array<class_Array>` **Array** **(** :ref:`PoolColorArray<class_PoolColorArray>` from **)**
Constructs an array from a :ref:`PoolColorArray<class_PoolColorArray>`.
----
- :ref:`Array<class_Array>` **Array** **(** :ref:`PoolVector3Array<class_PoolVector3Array>` from **)**
Constructs an array from a :ref:`PoolVector3Array<class_PoolVector3Array>`.
----
- :ref:`Array<class_Array>` **Array** **(** :ref:`PoolVector2Array<class_PoolVector2Array>` from **)**
Constructs an array from a :ref:`PoolVector2Array<class_PoolVector2Array>`.
----
- :ref:`Array<class_Array>` **Array** **(** :ref:`PoolStringArray<class_PoolStringArray>` from **)**
Constructs an array from a :ref:`PoolStringArray<class_PoolStringArray>`.
----
- :ref:`Array<class_Array>` **Array** **(** :ref:`PoolRealArray<class_PoolRealArray>` from **)**
Constructs an array from a :ref:`PoolRealArray<class_PoolRealArray>`.
----
- :ref:`Array<class_Array>` **Array** **(** :ref:`PoolIntArray<class_PoolIntArray>` from **)**
Constructs an array from a :ref:`PoolIntArray<class_PoolIntArray>`.
----
- :ref:`Array<class_Array>` **Array** **(** :ref:`PoolByteArray<class_PoolByteArray>` from **)**
Constructs an array from a :ref:`PoolByteArray<class_PoolByteArray>`.
----
.. _class_Array_method_append:
- void **append** **(** :ref:`Variant<class_Variant>` value **)**
Concatena un elemento al final del array (alias de :ref:`push_back<class_Array_method_push_back>`).
----
.. _class_Array_method_append_array:
- void **append_array** **(** :ref:`Array<class_Array>` array **)**
Appends another array at the end of this array.
::
var array1 = [1, 2, 3]
var array2 = [4, 5, 6]
array1.append_array(array2)
print(array1) # Prints [1, 2, 3, 4, 5, 6].
----
.. _class_Array_method_back:
- :ref:`Variant<class_Variant>` **back** **(** **)**
Returns the last element of the array. Prints an error and returns ``null`` if the array is empty.
\ **Note:** Calling this function is not the same as writing ``array[-1]``. If the array is empty, accessing by index will pause project execution when running from the editor.
----
.. _class_Array_method_bsearch:
- :ref:`int<class_int>` **bsearch** **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)**
Encuentra el indice de un valor existente (o el indice de insercion que mantiene el ordenamiento, si el valor no esta todavia presente en el array) usando busqueda binario. Opcionalmente, un especificador ``before`` puede ser pasado. Si ``false``, el indice devuelto vendra despues de todas las entradas existentes del valor en el array.
\ **Nota:** Llama al :ref:`bsearch<class_Array_method_bsearch>` en un array sin ordenar causara comportamientos inesperados.
----
.. _class_Array_method_bsearch_custom:
- :ref:`int<class_int>` **bsearch_custom** **(** :ref:`Variant<class_Variant>` value, :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func, :ref:`bool<class_bool>` before=true **)**
Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search and a custom comparison method declared in the ``obj``. Optionally, a ``before`` specifier can be passed. If ``false``, the returned index comes after all existing entries of the value in the array. The custom method receives two arguments (an element from the array and the value searched for) and must return ``true`` if the first argument is less than the second, and return ``false`` otherwise.
::
func cardinal_to_algebraic(a):
match a:
"one":
return 1
"two":
return 2
"three":
return 3
"four":
return 4
_:
return 0
func compare(a, b):
return cardinal_to_algebraic(a) < cardinal_to_algebraic(b)
func _ready():
var a = ["one", "two", "three", "four"]
# `compare` is defined in this object, so we use `self` as the `obj` parameter.
print(a.bsearch_custom("three", self, "compare", true)) # Expected value is 2.
\ **Note:** Calling :ref:`bsearch_custom<class_Array_method_bsearch_custom>` on an unsorted array results in unexpected behavior.
----
.. _class_Array_method_clear:
- void **clear** **(** **)**
Limpia el array. Esto es equivalente a usar :ref:`resize<class_Array_method_resize>` con un tamaño de ``0``.
----
.. _class_Array_method_count:
- :ref:`int<class_int>` **count** **(** :ref:`Variant<class_Variant>` value **)**
Devuelve el numer de veces que un elemento es encuentra en el array.
----
.. _class_Array_method_duplicate:
- :ref:`Array<class_Array>` **duplicate** **(** :ref:`bool<class_bool>` deep=false **)**
Devuelve una copia del array.
Si ``deep`` es ``true``, a copia profunda es ejecutada: todos los arrays anidados y diccionarios son duplicados y no seran compartidos con el array original. Si ``false``, una copia superificial es dada y las referencias a los arrays anidados y los diccionarios son mantenidos, por lo que modificar un sub-array or diccionario en la copia tambien cambiara estas referencias en el array fuente.
----
.. _class_Array_method_empty:
- :ref:`bool<class_bool>` **empty** **(** **)**
Devuelve ``true`` si el array es vacio.
----
.. _class_Array_method_erase:
- void **erase** **(** :ref:`Variant<class_Variant>` value **)**
Removes the first occurrence of a value from the array. To remove an element by index, use :ref:`remove<class_Array_method_remove>` instead.
\ **Note:** This method acts in-place and doesn't return a value.
\ **Note:** On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
----
.. _class_Array_method_find:
- :ref:`int<class_int>` **find** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)**
Busca el array por un valor y devuelve su indice o ``-1`` sino se encuentra. Opcionalmente, el indice de busqueda inicial puede ser pasado.
----
.. _class_Array_method_find_last:
- :ref:`int<class_int>` **find_last** **(** :ref:`Variant<class_Variant>` value **)**
Busca el array en orden inverso por un valor y devuelve su indice o ``-1`` sino es encontrado.
----
.. _class_Array_method_front:
- :ref:`Variant<class_Variant>` **front** **(** **)**
Returns the first element of the array. Prints an error and returns ``null`` if the array is empty.
\ **Note:** Calling this function is not the same as writing ``array[0]``. If the array is empty, accessing by index will pause project execution when running from the editor.
----
.. _class_Array_method_has:
- :ref:`bool<class_bool>` **has** **(** :ref:`Variant<class_Variant>` value **)**
Returns ``true`` if the array contains the given value.
::
["inside", 7].has("inside") # True
["inside", 7].has("outside") # False
["inside", 7].has(7) # True
["inside", 7].has("7") # False
\ **Note:** This is equivalent to using the ``in`` operator as follows:
::
# Will evaluate to `true`.
if 2 in [2, 4, 6, 8]:
pass
----
.. _class_Array_method_hash:
- :ref:`int<class_int>` **hash** **(** **)**
Returns a hashed integer value representing the array and its contents.
\ **Note:** Arrays with equal contents can still produce different hashes. Only the exact same arrays will produce the same hashed integer value.
----
.. _class_Array_method_insert:
- void **insert** **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)**
Inserta un nuevo elemento en la posisción dada en el array. La posición debe ser valida, o al final de el array(``pos == size()``.
\ **Note:** este metodo actua en el lugar y no devuelve ningún valor.
\ **Note:** en arrays largos, este metodo va a ser mas lento si el elemento incertado esta cerca al inicio del array (índice 0). Esto es por que todos los elementos despues del elemento incertado tienen que ser reindisados.
----
.. _class_Array_method_invert:
- void **invert** **(** **)**
Invierte el orden de los elementos en el array.
----
.. _class_Array_method_max:
- :ref:`Variant<class_Variant>` **max** **(** **)**
Devuelve el maximo valor contenido en el array si todos los elementos son de tipos comparables. Si los elementos no pueden ser comparados, ``null`` es devuelto.
----
.. _class_Array_method_min:
- :ref:`Variant<class_Variant>` **min** **(** **)**
Devuelve el minimo valor contenido en el array si todos los elementos son de tipos comparables. Si los elementos no pueden ser comparados, ``null`` es devuelto.
----
.. _class_Array_method_pop_at:
- :ref:`Variant<class_Variant>` **pop_at** **(** :ref:`int<class_int>` position **)**
Removes and returns the element of the array at index ``position``. If negative, ``position`` is considered relative to the end of the array. Leaves the array untouched and returns ``null`` if the array is empty or if it's accessed out of bounds. An error message is printed when the array is accessed out of bounds, but not when the array is empty.
\ **Note:** On large arrays, this method can be slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex the array's elements that are located after the removed element. The larger the array and the lower the index of the removed element, the slower :ref:`pop_at<class_Array_method_pop_at>` will be.
----
.. _class_Array_method_pop_back:
- :ref:`Variant<class_Variant>` **pop_back** **(** **)**
Removes and returns the last element of the array. Returns ``null`` if the array is empty, without printing an error message. See also :ref:`pop_front<class_Array_method_pop_front>`.
----
.. _class_Array_method_pop_front:
- :ref:`Variant<class_Variant>` **pop_front** **(** **)**
Removes and returns the first element of the array. Returns ``null`` if the array is empty, without printing an error message. See also :ref:`pop_back<class_Array_method_pop_back>`.
\ **Note:** On large arrays, this method is much slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`pop_front<class_Array_method_pop_front>` will be.
----
.. _class_Array_method_push_back:
- void **push_back** **(** :ref:`Variant<class_Variant>` value **)**
Appends an element at the end of the array. See also :ref:`push_front<class_Array_method_push_front>`.
----
.. _class_Array_method_push_front:
- void **push_front** **(** :ref:`Variant<class_Variant>` value **)**
Adds an element at the beginning of the array. See also :ref:`push_back<class_Array_method_push_back>`.
\ **Note:** On large arrays, this method is much slower than :ref:`push_back<class_Array_method_push_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`push_front<class_Array_method_push_front>` will be.
----
.. _class_Array_method_remove:
- void **remove** **(** :ref:`int<class_int>` position **)**
Removes an element from the array by index. If the index does not exist in the array, nothing happens. To remove an element by searching for its value, use :ref:`erase<class_Array_method_erase>` instead.
\ **Note:** This method acts in-place and doesn't return a value.
\ **Note:** On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
----
.. _class_Array_method_resize:
- void **resize** **(** :ref:`int<class_int>` size **)**
Cambiar el tamaño del array para contener un numero diferente de elementos. Si el array es menor, los elementos so limipiados, si mayor, los nuevos elementos son ``null``.
----
.. _class_Array_method_rfind:
- :ref:`int<class_int>` **rfind** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)**
Busca el array en orden inverso. Opcionalmente, un indice de comienzo de busqueda puede ser pasado. Si negacion, el indice de comienzo es considerado relativo al final del array.
----
.. _class_Array_method_shuffle:
- void **shuffle** **(** **)**
Baraja el array de forma que los items tengan un orden aleatorio. Este metodo uso el generador de numeros globales aleatorios comun de metodos como :ref:`@GDScript.randi<class_@GDScript_method_randi>`. Llamar :ref:`@GDScript.randomize<class_@GDScript_method_randomize>` para asegurar que una nueva semilla sea utilizada cada vez si tu no quieres reproducir el orden de los items.
----
.. _class_Array_method_size:
- :ref:`int<class_int>` **size** **(** **)**
Devuelve el numer de elementos en el array.
----
.. _class_Array_method_slice:
- :ref:`Array<class_Array>` **slice** **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)**
Duplica el subset descrito en la funcion y lo devuelve en un array, copiando profundamente el array si ``deep`` es ``true``. Bajos y altos indices estan incluidos, cone el ``step]`` describiendo el cambio entre indices mientras se trocean.
----
.. _class_Array_method_sort:
- void **sort** **(** **)**
Ordena el array.
\ **Nota:** Las strings son ordenadas en orden alfabetico ( opuesto al orden natural). Esto puede llevar a comportamientos inesperados cuando se ordene un array de strings con una sequencia de numeros, considera lo siguiente:
::
var strings = ["string1", "string2", "string10", "string11"]
strings.sort()
print(strings) # Imprime [string1, string10, string11, string2]
----
.. _class_Array_method_sort_custom:
- void **sort_custom** **(** :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func **)**
Sorts the array using a custom method. The arguments are an object that holds the method and the name of such method. The custom method receives two arguments (a pair of elements from the array) and must return either ``true`` or ``false``.
For two elements ``a`` and ``b``, if the given method returns ``true``, element ``b`` will be after element ``a`` in the array.
\ **Note:** You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Doing so will result in unexpected behavior.
::
class MyCustomSorter:
static func sort_ascending(a, b):
if a[0] < b[0]:
return true
return false
var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
my_items.sort_custom(MyCustomSorter, "sort_ascending")
print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]].
.. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
.. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
.. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`