Jump to content

table.sort problem


John Smith

Recommended Posts

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?

Link to comment
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?

Link to comment

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) 

Link to comment

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

Link to comment

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

Link to comment
  • Moderators
    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) 

Link to comment
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..

Link to comment

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

Link to comment
  • Moderators

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.

Link to comment
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.

Link to comment

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