[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Sort function modifies the table being sorted?
- From: Dimiter 'malkia' Stanev <malkia@...>
- Date: Wed, 13 Jun 2012 23:59:10 -0700
Think of the sorting routine as a black box.
The sorting routine might've decided to copy the whole array somewhere,
and empty 'o' all the time. You should not access these elements.
The sorting routine also expects strict ordering. The only thing you
have to do is just return whether something is bigger or smaller than
anything else - nothing else.
Probably the 'qsort' algorithm is used (but that's just another detail),
and if you invalidate the ordering the algorithm might not finish, or do
You are doing exactly that, e.g. relying on side effects. There is
nothing reasonable about that.
On 6/13/2012 11:22 PM, Paul K wrote:
hm, if o[a] does not exist, I should get 9 in both cases. The
difference should be only when o[a] does exist as far as I can tell.
In any case, I would not expect to get nil as one of the values being sorted.
I understand "don't do that" advice, but in this case it was a
sensible thing to do and I'd like to know more on why this may be not
working before I mark it as a dead end.
On Wed, Jun 13, 2012 at 10:54 PM, Dirk Laurie<email@example.com> wrote:
2012/6/14 Paul K<firstname.lastname@example.org>:
I suspect so too, but (1) it's not stated anywhere that it's against
the rules and (2) changing to "o[a] and 0 or 9" fixes it, which should
make no difference.
It does make a difference. In the course of the sorting process, you reach
a point where o[a] does not exist. As coded now, you will get 0, before
you would have got 9. 0 is not logically false in Lua.