lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


That gives me an idea.

I can probably create my own sorted iterator spairs() that will pre-sort the keys in some way. It can be smart to handle multiple types, like a mix of strings and numbers, even some types of user data if those can be ordered deterministically. It will not be very fast but I don't expect to use it for more than a few dozen items.

In fact if the number of items is quite small I can do the sorting directly in the the iterator's stack, and not even bother creating a separate table to hold the order.

-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Javier Guerra Giraldez
Sent: Friday, February 08, 2013 12:49 PM
To: Lua mailing list
Subject: Re: Deterministic table iteration

On Fri, Feb 8, 2013 at 3:37 PM, Ivo Beltchev <ivo@ibeltchev.com> wrote:
> I’d like to know in what cases I can count on deterministic order of 
> table iteration. I’m pretty sure if the keys are objects that are 
> stored in memory (like tables, threads, closures, or full user data), 
> the order is not deterministic. But what if the keys are limited to 
> simple types, like numbers, strings, booleans, light user data? Is the 
> order going to be always the same?

no.  if you want deterministic order, you can easily keep a copy of the keys on the array part, like table.sort() result.



--
Javier