Lua Custom sort function

Source: Internet
Author: User
Tags lua

Speaking of the more commonly used sort functions in the table Library of Lua--table.sort (), the first parameter represents the table to be sorted, the second parameter is optional, and if there is no second argument, the function will sort the table element from small to large. I would mainly say the second parameter.

The second argument is the function type, which receives two table elements and should return TRUE if you want the first parameter to be in the sort result before the second argument. Then the most intuitive understanding is to look at the code, as follows is a way of writing

Local tab = {3,5,4,1,2}

local function comp (b)
	return  a > B

Table.sort (Tab,comp)
for I =1, #tab do 
	print (tab[i])

Another way of writing is anonymous function writing, relatively simple, I am not on the code here, the intention of the reader can do their own.

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: 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.