@@ -1473,12 +1473,14 @@ defmodule Enum do
1473
1473
end
1474
1474
1475
1475
@ doc """
1476
- Returns a sorted list of collection elements. Uses the merge sort algorithm.
1476
+ Sorts the collection according to Elixir's term ordering.
1477
+
1478
+ Uses the merge sort algorithm.
1477
1479
1478
1480
## Examples
1479
1481
1480
1482
iex> Enum.sort([3, 2, 1])
1481
- [1,2, 3]
1483
+ [1, 2, 3]
1482
1484
1483
1485
"""
1484
1486
@ spec sort ( t ) :: list
@@ -1491,14 +1493,27 @@ defmodule Enum do
1491
1493
end
1492
1494
1493
1495
@ doc """
1494
- Returns a list of collection elements sorted by the given function.
1496
+ Sorts the collection by the given function.
1495
1497
1496
- Uses the merge sort algorithm.
1498
+ This function uses the merge sort algorithm. The given function
1499
+ must return false if the first argument is less than right one.
1497
1500
1498
1501
## Examples
1499
1502
1500
1503
iex> Enum.sort([1, 2, 3], &(&1 > &2))
1501
- [3,2,1]
1504
+ [3, 2, 1]
1505
+
1506
+ The sorting algorithm will be stable as long as the given function
1507
+ returns true for values considered equal:
1508
+
1509
+ iex> Enum.sort ["some", "kind", "of", "monster"], &(byte_size(&1) <= byte_size(&2))
1510
+ ["of", "some", "kind", "monster"]
1511
+
1512
+ If the function does not return true, the sorting is not stable and
1513
+ the order of equal terms may be shuffled:
1514
+
1515
+ iex> Enum.sort ["some", "kind", "of", "monster"], &(byte_size(&1) < byte_size(&2))
1516
+ ["of", "kind", "some", "monster"]
1502
1517
1503
1518
"""
1504
1519
@ spec sort ( t , ( element , element -> boolean ) ) :: list
0 commit comments