John Smith Posted August 7, 2015 Share Posted August 7, 2015 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
GTX Posted August 7, 2015 Share Posted August 7, 2015 Try: table.sort(TEMP_TABLE, function(a, b) return a[1] < b[1] end) Then just set TEMP_TABLE to toptimes variable. Link to comment
John Smith Posted August 7, 2015 Author Share Posted August 7, 2015 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. Link to comment
GTX Posted August 7, 2015 Share Posted August 7, 2015 Just use table.sort, you don't need to loop or anything. If you're using MySQL you can use ORDER BY. Link to comment
John Smith Posted August 7, 2015 Author Share Posted August 7, 2015 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? Link to comment
GTX Posted August 7, 2015 Share Posted August 7, 2015 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. Link to comment
John Smith Posted August 7, 2015 Author Share Posted August 7, 2015 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
GTX Posted August 7, 2015 Share Posted August 7, 2015 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
John Smith Posted August 7, 2015 Author Share Posted August 7, 2015 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
GTX Posted August 7, 2015 Share Posted August 7, 2015 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 Link to comment
John Smith Posted August 7, 2015 Author Share Posted August 7, 2015 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 IIYAMA Posted August 7, 2015 Moderators Share Posted August 7, 2015 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
Moderators IIYAMA Posted August 7, 2015 Moderators Share Posted August 7, 2015 and yes, you can sort by characters/words: outputChatBox(tostring("a" < "b")) -- true outputChatBox(tostring("b" < "a")) -- false Link to comment
John Smith Posted August 7, 2015 Author Share Posted August 7, 2015 Oh wow. Thanks guys for help, it worked now 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! Link to comment
John Smith Posted August 10, 2015 Author Share Posted August 10, 2015 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
MTA Team botder Posted August 10, 2015 MTA Team Share Posted August 10, 2015 For any sorting you have to compare 2 values. The parameters 'a' and 'b' are values from your table (you shouldn't care about index) and table.sort calls that function with 2 parameters. Link to comment
John Smith Posted August 10, 2015 Author Share Posted August 10, 2015 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 IIYAMA Posted August 10, 2015 Moderators Share Posted August 10, 2015 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
novo Posted August 10, 2015 Share Posted August 10, 2015 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
John Smith Posted August 10, 2015 Author Share Posted August 10, 2015 Okay, thanks guys! I've managed to fix my problem now Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now