Browse Source

Corrected documentation for 'table.sort'

The sort function must define a (strict) weak order for a correct
sorting. A partial order is not enough.
pull/25/head
Roberto Ierusalimschy 4 years ago
parent
commit
825ac8eca8
  1. 16
      manual/manual.of

16
manual/manual.of

@ -7821,19 +7821,19 @@ from @T{list[1]} to @T{list[#list]}.
If @id{comp} is given, If @id{comp} is given,
then it must be a function that receives two list elements then it must be a function that receives two list elements
and returns true when the first element must come and returns true when the first element must come
before the second in the final order before the second in the final order,
(so that, after the sort, so that, after the sort,
@T{i < j} implies @T{not comp(list[j],list[i])}). @T{i <= j} implies @T{not comp(list[j],list[i])}.
If @id{comp} is not given, If @id{comp} is not given,
then the standard Lua operator @T{<} is used instead. then the standard Lua operator @T{<} is used instead.
Note that the @id{comp} function must define The @id{comp} function must define a consistent order;
a strict partial order over the elements in the list; more formally, the function must define a strict weak order.
that is, it must be asymmetric and transitive. (A weak order is similar to a total order,
Otherwise, no valid sort may be possible. but it can equate different elements for comparison purposes.)
The sort algorithm is not stable: The sort algorithm is not stable:
elements considered equal by the given order Different elements considered equal by the given order
may have their relative positions changed by the sort. may have their relative positions changed by the sort.
} }

Loading…
Cancel
Save