Jump to content

table.sort problem


John Smith

Recommended Posts

Posted

Hey

I'm using this piece of code to sort table's numbers from lowest to highest but doesn't work.

function sortRecords() 
   local TOPTIMES_COPY = toptimes; 
   local TEMP_TABLE = {};  
    
   for _,v in ipairs(TOPTIMES_COPY) do 
      local _ARG_0_ = unpack(v);  
      table.insert(TEMP_TABLE,_ARG_0_); 
   end; 
    
   table.sort(TEMP_TABLE); 
    
   for _,v in ipairs(TOPTIMES_COPY) do 
      for _,value in ipairs(TEMP_TABLE) do 
         local _,_ARG_1_ = unpack(v); 
         v = {value,_ARG_1_}; 
      end; 
   end; 
    
   toptimes = TOPTIMES_COPY; 
    
end; 

My toptimes table at the moment consists of 2 type of data

milisecond time, player name

I'm using above code to extract only numbers from the toptimes table so that i can sort miliseconds from lowest to highest number and then afterwards replacing toptimes table with new, sorted and modified toptimes table

The problem is that table.sort doesn't sort my table numbers at all. They remain the same.

Example:

Numbers (in order) 900,100,1,69 don't get sorted to 1,69,100,900 but instead they remain untouched.

Anyone knows what's the problem?

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

Posted

Try:

table.sort(TEMP_TABLE, function(a, b) return a[1] < b[1] end) 

Then just set TEMP_TABLE to toptimes variable.

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

I might have misunderstood you or something, but when i replace my line with yours, it doesn't logically work as a,b is nil

Also, why doesn't table.sort(table) work? I've tried runcode examples of tables with it, and it works fine, but not in my script.

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

Posted

Just use table.sort, you don't need to loop or anything. If you're using MySQL you can use ORDER BY.

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

I prefer saving data to tables rather than using database solutions.

I'm already using table.sort (You can see it in my first post)

table.sort(TEMP_TABLE); 

aaaand it doesn't work... who knows why?

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

Posted

It does work, you just don't use it correctly. It is better and convenient using ORDER BY than table.sort, because if you use table.sort, it needs to process data twice.

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted
It does work, you just don't use it correctly. It is better and convenient using ORDER BY than table.sort, because if you use table.sort, it needs to process data twice.

Yes. And that data processing takes really little time (not even noticeable).

Besides, this sorting things only happens when there's an update to the toptimes so performance is not really an issue here, but thank you for letting me know of this.

And yes. table.sort should work, but doesn't. That's why I made this topic in first place.

  
-- Output 
[1] = {900,WonderfulNick52} 
[2] = {100,WonderfulNick52} 
[3] = {1,WonderfulNick52} 
[4] = {69,WonderfulNick52} 
  
-- this is toptimes table data, TEMP_TABLE contains only the numbers 
  

This output remains the same even before and after using table.sort, do you happen to know why?

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

Posted

It does work. I used:

local tablex = { 
[1] = {900,"WonderfulNick52"}, 
[2] = {100,"WonderfulNick52"}, 
[3] = {1,"WonderfulNick52"}, 
[4] = {69,"WonderfulNick52"} 
} 
  
table.sort(tablex, function(a,b) return a[1] < b[1] end) 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

I've tried your thing and it did sort...a bit

I've replaced the function code in my first post with this table.sort line of yours and used toptimes as the table but there's one order mistake in it

--Output 
[1] = {1,WonderfulNick52} 
[2] = {100,WonderfulNick52} 
[3] = {69,WonderfulNick52} 
[4] = {900,WonderfulNick52} 
  

69 should be on index 2, and 100 on index 3 :/

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

Posted

Remove the loops. They're completely unnecessary.

function sortRecords() 
    table.sort(toptimes, function(a, b) return a[1] < b[1] end) -- Note that a[1] must exist and must be your timestamp. 
end 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

That's exactly how my function looks like right now..

I've tried even adding more numbers to the table, and look it's like if the table.sort function can notice only first 2 characters of the number, but if its above 99 (has 3 or more characters, it breaks down)

--Output 
[1] = {1,WonderfulNick52} 
[2] = {100,WonderfulNick52} 
[3] = {3,WonderfulNick52} 
[4] = {53,WonderfulNick52} 
[5] = {69,WonderfulNick52} 
[6] = {900,WonderfulNick52} 
[7] = {99,WonderfulNick52} 
[8] = {993,WonderfulNick52} 
  

I'm stuck, I don't know what to do..

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

  • Moderators
Posted
    local tablex = { 
    [1] = {"900","WonderfulNick52"}, 
    [2] = {"100","WonderfulNick52"}, 
    [3] = {"1","WonderfulNick52"}, 
    [4] = {"69","WonderfulNick52"} 
    } 
      
    table.sort(tablex, function(a,b) return a[1] < b[1] end) 
  
for i=1,#tablex  do 
outputChatBox(tablex[i][1]) 
end 

That is because you are using strings, not numbers.

1 
100 
69 
900 
  

So sort it like this:

table.sort(tablex, function(a,b) return tonumber(a[1]) < tonumber(b[1]) end) 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted

and yes, you can sort by characters/words:

outputChatBox(tostring("a" < "b")) -- true 
outputChatBox(tostring("b" < "a")) -- false 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

Oh wow. Thanks guys for help, it worked now :D

I didn't thought of this since i though table.sort would return some type of an error table.sort [expected number, got string]

Anyway, thanks fellas!

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

Posted
table.sort(toptimes, function(a, b) return a[1] < b[1] end) 

could anyone explain please to me what is a,b and how it is defined? i need to understand it because i'm facing an issue where it says that it's trying to compare nil with a number (meaning that a[1] is nil and b[1] is a number) and i don't really understand the definition of a,b so i cannot event attempt to fix it..

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

Posted

Yeah, that is what i already know. The thing that confuses me is that it compares 2 number values. <--

You see, my table sorta looks like this when it's filled in with data

toptimes = { 
{32423,"nickname"}, 
{12313,"nickname"}, 
}; 

So a,b values. Which ones would those be in table example above? I know that it should compare numbers, but i don't know how the process goes (i need to understand how things work in order to learn)

Also, i gotta know this because I'm getting an error in which it's trying to compare nil with a number, so to even try solving that issue, i gotta at least understand... well all of this a,b stuff

i hope that u understand what i'm saying and that you can help me

If you find my post useful or if it helped you, please like my post :)
Ingame name: ZoeN

560x95_FFFFFF_FF9900_000000_000000.png

  • Moderators
Posted

I personal see it like this: (others have different opinions about it)

A: can be anything in the table.

B: can also be anything in the table.

The only way to sort a table is by comparing data with each other.

So what are A and B?

[color=#0000FF]toptimes[/color] = { 
    [color=#FF0000][b]{[/b][/color]32423,"nickname"[color=#FF0000][b]},[/b][/color] 
    [color=#FF0000][b]{[/b][/color]12313,"nickname"[color=#FF0000][b]},[/b][/color] 
}; 

A and B are the data which are inside of the sorted table. Which are in this case tables too.

The table.sort function will be comparing and sort those data behind the scene. And the function(with the A and B) will be used in this process.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted
toptimes = { 
{32423,"nickname"}, 
{12313,"nickname"}, 
}; 
  
table.sort(toptimes, 
    function(a, b) 
        return a[1] < b[1] 
    end 
) 

table.sort basically needs minimum two values inside the input table, toptimes in this case, and then does compare these values and thus determines their order. We have got two sub-tables inside toptimes, and those tables are the ones being compared through table.sort. This is why [1] is used, we retrieve those table's index 1 value and then check which one is smaller/bigger. However, here is a similar thing I found you may have a look at.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...