Lua Table Sort

Source: Internet
Author: User

For LUA table sorting problems, it is generally sorted by value, using Table.sort (Needsorttable, func) (you can rewrite Func to suit your needs, otherwise the default is: If there are string,number types in the table, there will be errors due to the direct compare of the two types, so you need to write func to convert them, or you can add the appropriate logic in the Func to meet your ordering requirements.

Localtest_table = {2,1,3,"SORT","Sort"}  Table.sort(Test_table,function(A, b)return ToString(a) >ToString(b)End)   forKey,valueinch Pairs(test_table) Do      Print(Key,value)End  --The output is as follows:1Sort2SORT3    34    25    1

To sort a table by key values, just enter it, visually:

Localtest_table = {a=3, b=2, c=4, d=1}  LocalKey_table = {}  --Remove all the keys forKey,_inch Pairs(test_table) Do      Table.insert(Key_table,key)End  --sort All keysTable.sort(key_table) for_,keyinch Pairs(key_table) Do      Print(Key,test_table[key])End

But this is only possible when the print input is done, and the table itself does not change, so this is undesirable; we can change the iteration factor of the generic for to achieve this goal:

functionIpairs2 (a)returnIter,a,0EndLocal functionITER (A, i) I= i +1    Localv =A[i]ifV Then        returnI, vElse        return Nil,Nil    EndEnda= {" One"," Both","three"} forKvinchIpairs2 (a) Do    Print(k, v)End--the output is:--1 One--2--3 Three

This is what we want to do, but this only supports tables that are shaped as follows (that is, tables that are placed in the table array section, but not Weili in the hash section); So you need something like this:

Lines ={Name="Jeff",    {"Pairsbykeys"}, Luah_set=Ten, Luah_get= -, Luah_present= -,}functionPairsbykeys (T, f)LocalA = {}     forNinch Pairs(t) Do Table.insert(A, N)End    Table.sort(A, F)Locali =0                 --iterator Variable    LocalITER =function()--iterator functioni = i +1       ifA[i] = =Nil  Then return Nil       Else returnA[i], T[a[i]]End    End    returnITEREndfunctionSortfunc (A, b)if ToString(a) >ToString(b) Then         return true    EndEnd forName, lineinchPairsbykeys (lines, Sortfunc) Do    Print(name, line)End

The output results are as follows:

name    jeffluah_set    luah_present    luah_get       1in 0.1s]    

This allows the table to be sorted by key-value pairs; For articles on generics, see this article: Click here

Refer to Bolg:click here

Lua Table Sort

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.