Lua implements a sort comparison method by itself, throwing an error invalid order function for sorting

Source: Internet
Author: User
Tags lua

Tomorrow the new feature is on the go, the result just suddenly QA said the project throws a mistake. Holding the grass, frightened immediately out of a sweat.

Check the error, found that it may be written by their own unstable sort caused. I feel that I should be. After the sorting method is written as stable, the code is compiled into the mobile phone, and the wood is wrong to run. Why do you want to sort the ordered data sent by the server? Brain pumping is unclear.

The following is a summary of the LUA library for others to turn. (There are too many places to study)

LUA's Table Library

List of functions:
Table.insert (table,[POS,] value)
Table.remove (table[, POS])
Table.concat (table[, sep[, i[, J]])
Table.sort (table[, comp])


1. Insert and remove can only be used for insertion and removal of array elements, and the subsequent elements are aligned when inserted and removed.
So when inserting and removing in the for loop, pay attention to whether the items are missing when inserted and removed:
Local t = {1,2,3,3,5,3,6}
For i,v in Ipairs (t) do
If v = = 3 Then
Table.remove (T,i)
End
End
--Error, fourth 3 is not removed, Ipairs internally maintains a variable record of the Traverse, remove the third number 3, Ipairs the next return value is 5 instead of 3

Local t = {1,2,3,3,5,3,6}
For I=1, #t do
If t[i] = = 3 Then
Table.remove (T,i)
i = I-1
End
End
--Error, i=i-1 This code is useless, the value of I is always from 1 to #t, the value of modify I in the For loop does not work

Local t = {1,2,3,3,5,3,6}
For i= #t, 1,-1 does
If t[i] = = 3 Then
Table.remove (T,i)
End
End
--right, traversing backwards from backwards

Local t = {1,2,3,3,5,3,6}
Local i = 1
While T[i] do
If t[i] = = 3 Then
Table.remove (T,i)
Else
i = i+1
End
End
--correct, control whether the value of I increases

   
2. Concat can stitch the array portion of a table into a string, separated by a seq.  
    LUA does not store strings in the same way as C, each string in Lua is a separate copy, and stitching two strings creates a new copy, which affects performance if the splicing operation is particularly numerous:  
        Local beginTime = Os.clock ()  
         Local str = ""  
        for I=1, 30000 do 
             str = str.. i 
        end 
        Local endTime = Os.clock ()  
        print (endtime-begintime)  
       -consumes 0.613 seconds, resulting in 30,000 string copies, but only the last one is useful

Local beginTime = Os.clock ()
Local T = {}
For I=1, 30000 do
T[i] = i
End
Local str = table.concat (t, "")
Local endTime = Os.clock ()
Print (Endtime-begintime)
--consumes 0.024 seconds and uses concat to stitch strings together, producing only a copy of the string


3. Sort can sort the elements of the table array section, need to provide comp function, comp (A, B) if a should be in front of B, Comp returns TRUE.
Note that in case of a==b, be sure to return false:
Local function Comp (A, B)
Return a <= b
End
Table.sort (T,comp)
--Error, an exception may occur: Attempt to compare number with nil

Local function Comp (A, B)
If a = nil or b = Nil Then
return False
End
Return a <= b
End
Table.sort (T,comp)
--Error, an exception may occur: Invalid order function for sorting
may not report the exception, but the result is wrong;
The reason that A==b returns true causes these problems because Table.sort does not do boundary detection when implementing a quick sort:
for (;;) {
while (Lua_rawgeti (l, 1, ++i), Sort_comp (L,-1,-2)) {//undetected boundary, I will always increase
if (i>=u) lual_error (L, "Invalid order function for sorting");
Lua_pop (L, 1);
}
while (Lua_rawgeti (l, 1,--j), Sort_comp (L,-3,-1)) {//undetected boundary, J will always decrease
if (j<=l) Lual_error (L, "Invalid order function for sorting");
Lua_pop (L, 1);
}
if (j<i) {
Lua_pop (L, 3);
Break
}
Set2 (L, I, J);
}
Looking at the code above, if A==b returns true and several values on the boundary are equal, Sort_comp cannot prevent I from continuing to grow until an exception is thrown beyond the bounds attempt to compare number with nil, even if we do not Null judgment, also causes an exception because I exceeds the bounds invalid order function for sorting
Quick sorting is what, lua how to achieve a quick sort, you can refer to the description of the LUA source code, here is not much introduction;

Lua implements a sort comparison method by itself, throwing an error invalid order function for sorting

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.