Can we count on [list of values] order persisting?

I have some code as follows. The point is to let the player replay a short game with different possibilities for a final puzzle at the end, so they get all of them, and once they do, the game congratulates them for solving everything.

puzzle-type is a kind of puzzle. The puzzle-types are type-1, type-2, type-3 and type-4.

A room has a puzzle-type of endgame-val.

puz-list is a list of puzzle-types variable.

when play begins:
    let puz-list be the list of puzzle-types;
    sort puz-list in random order;

puzzle-index is a number that varies.

to bump-puzzle-up:
    increment puzzle-index;
    if puzzle-index > number of puzzle-types, now puzzle-index is 1;
    now current-puzzle is entry puzzle-index in puz-list;
    (place the bad guy in a random room with endgame-val of current-puzzle)

Now this all works and will get me the test cases I want. But for a big test file, I’d like to have the puzzle types lined up in order, as the solution for #2 is different from #1, etc.

Of course, I can create a test command to force a certain puzzle-type at any one time, but if I can get away with just 1 command to say “do things in order,” that’d be nice. Plus I’m genuinely curious about stability.

Running

every turn: say "[the list of puzzle-types]."

Reliably gives puzzles in the same order. So I have these questions:

  1. is this a reasonable expectation in general? Is this sort of like how table row order is preserved when you sort tables, if two rows have identical values?
  2. is it bad coding practice to rely on this expectation and, if so, what should I do to make my code work right?
  3. I see a similar behavior with lists of items. So I’d assume the same answer would apply for items as for values, too?

Thanks!

I believe that the underlying structure of lists should be static in the absence of manipulation of the list itself, so the order should be retained if you do nothing to change it.

1 Like

say list of KIND is consistent; it’s the order the objects are created. (But Inform creates objects in the order that they’re mentioned in your source code. Except some mentions count and others don’t, and it’s hard to keep track. So be careful.)

say list of THINGS in CONTAINER shows them in the order that they were inserted. Or maybe it was reverse order, I don’t remember.

When you make a “list of FOO” variable, its order is stable; it’s the order that you add things to the list. (But remember that say the list of FOO does not use a list variable.)

3 Likes