lua-users home
lua-l archive

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

It's not a bad idea, it's especially useful for those sequences with nils everyone is rambling on about nowadays. If you have something like
for i,v in ipairs{5,nil,nil,nil,1} do t[i] = v end --needed to mess up the length
then table.sort will sort only the first element and you end up with the same table you started with. It's the same thing as with table.unpack: if you have some extra knowledge about the table's length (eg.: extra length field from table.pack or using vararg in 5.1) then you can use that in your sorting algorithm. Of course you would need a comparison function that can deal with nil.

On Sat, Aug 27, 2016 at 12:54 AM, Soni L. <> wrote:

On 26/08/16 02:51 AM, Dirk Laurie wrote:
I wanna sort a Lua table without a concept of length.

I've been wondering: why does table.sort not have optional
beginning and starting points?

Beginning and... starting points?

Huh? O_o

1. table.concat, table.move, table.unpack all have it.
2. It makes a lot of sense (in fact, many sorting algorithms
operate that way).
3. It is a very easy thing to implement.
4. The maligned concept of length can be sidestepped.

Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.