Comparison functions of table. Sort in Lua

Source: Internet
Author: User

The comprehension of table. Sort's comparison function is not in place, and the result hurts me. t

 

This is the case in the Lua 5.1 manual:

 

Table. Sort (Table [, comp])

 

Sorts table elements in a given order, in-place, from table [1] to table [N], where N is the length of the table. if comp is given, then it must be a function that includes es two table elements, and returns true when the first is less than the second (so that not comp (A [I + 1], a [I]) will be true after the sort ). if comp is not given, then the standard Lua operator <is used instead.

 

The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.

 

Returns true when the first isLessThe second

 

What should I return if the two parameters are equal? False!

 

The key is this. False must be returned; otherwise, the sorting result is incorrect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.