.:HyPeX:. Posted March 27, 2014 Share Posted March 27, 2014 Guys should i add this to useful functions? I've not tested it yet, so if anyone finds something wrong, please tell me. Idea: Give a table with Names and Values, and return a table ordered from smalller to bigger or bigger to smaller (by default smaller to bigger) and with a limit (If specified Max = 3 then table returned would only contain the 3 smallest or biggest.) Usage: UnOrderedTable = {} OrderedTable = PositionOrdering(UnOrderedTable, true, 5) Would return: A table ordered from biggest to smallest with a limit of 5. And finally, the function: function PositionOrdering(Table, Smaller,Max) if ( Table ) and ( type( Table ) == "table" ) then PTableValues = {} PTable = {} for i,v in pairs(Table) do PTableVales[i] = v table.insert(PTable, v) end if Smaller == true then table.sort(PTable, function(a,b) return a>b end) else table.sort(PTable) end TableReturn = {} for i,v in ipairs(PTable) do for k,s in pairs(PTableValues) do if v == s then TableReturn[k] = i break end end end if not Max then return TableReturn else count = 0 TableReturn2 = {} for i,v in pairs(TableReturn) do if count >= Max then break end TableReturn2[i] = v count = count + 1 end return TableReturn2 end else return false end end Link to comment
Moderators IIYAMA Posted March 27, 2014 Moderators Share Posted March 27, 2014 Use more locals at the right places or people can get problems. Especially when you use the variable (which is used often) as a global. Also: for i,v in ipairs(PTable) do for k,s in pairs(PTableValues) do if v == s then TableReturn[k] = i break end end end Why do you do this? What is the use of it, when the tables contain the same information but at a differed array location? Link to comment
.:HyPeX:. Posted March 27, 2014 Author Share Posted March 27, 2014 Use more locals at the right places or people can get problems. Especially when you use the variable (which is used often) as a global. Also: for i,v in ipairs(PTable) do for k,s in pairs(PTableValues) do if v == s then TableReturn[k] = i break end end end Why do you do this? What is the use of it, when the tables contain the same information but at a differed array location? Becouse, i cant sort an unindexed table, but an indexed table cant contain information. Supposing Players have different values, you can search them back by their values and have also their position. EDIT: Added some locals, and changed names to longer ones wich hardly would be used.. and also organized it a bit better- function PositionOrdering(CoreSoftyTable, CoreSoftySmaller,CoreSoftyMax) if ( CoreSoftyTable ) and ( type( CoreSoftyTable ) == "table" ) then local CoreSoftyPTableValues = {} local CoreSoftyPTable = {} for i,v in pairs(Table) do function PositionOrdering(CoreSoftyTable, CoreSoftySmaller,CoreSoftyMax) if ( CoreSoftyTable ) and ( type( CoreSoftyTable ) == "table" ) then local CoreSoftyPTableValues = {} local CoreSoftyPTable = {} for i,v in pairs(Table) do CoreSoftyPTableValues[i] = v table.insert(CoreSoftyPTable, v) end if CoreSoftySmaller then table.sort(CoreSoftyPTable, function(a,b) return a>b end) else table.sort(CoreSoftyPTable) end CoreSoftyTableReturn = {} for i,v in ipairs(CoreSoftyPTable) do for k,s in pairs(CoreSoftyPTableValues) do if v == s then CoreSoftyTableReturn[k] = i break end end end if not CoreSoftyMax then return CoreSoftyTableReturn else local CoreSoftycount = 0 CoreSoftyTableReturn2 = {} for i,v in pairs(CoreSoftyTableReturn) do if CoreSoftycount >= CoreSoftyMax then break end CoreSoftyTableReturn2[i] = v CoreSoftycount = CoreSoftycount + 1 end return CoreSoftyTableReturn2 end else return false end end Link to comment
myonlake Posted March 28, 2014 Share Posted March 28, 2014 Not sure why the code is so long, it's not a big deal to order a table from smallest value to largest value or the other way around. local my_values = { 1, 9, 7, 8, 2, 5, 4, 3, 6 } --[[ -- table order_table( table _table [, bool direction ] ) -- direction: true -> ordered from smallest to largest -- direction: false -> ordered from largest to smallest --]] function order_table( _table, direction ) if ( type( direction ) ~= "boolean" ) then direction = true end table.sort( _table, ( direction and function( a, b ) return a < b end or function( a, b ) return a > b end ) ) return _table end local _concat = table.concat( order_table( my_values, true ), ", " ) print( _concat ) Returns: 1, 2, 3, 4, 5, 6, 7, 8, 9 And the other way around: 9, 8, 7, 6, 5, 4, 3, 2, 1 If you're dealing with tables that don't index up accordingly, then I suggest you fix that issue by indexing everything accordingly. It saves you a lot of time anyhow when you've got it all indexed right. But as far as I am concerned, even if you made it that way it shouldn't be that long code to perform a couple for loops. 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