Sorting aggregate data structures

Apologies; I'm completely failing to understand the sorting bit here.

How do I sort this array randomly so the keys stay paired up?

Code:
string [int][int] list;

list[0][0] = "Alice";
list[1][0] = "Bob";
list[2][0] = "Charlie";

print("Unsorted");
dump(list); // Prints the list as above

print("Sorted");
sort list by random(1000000);
dump(list); // Prints the list with, alas, re-ordered pairs.
 

fronobulax

Developer
Staff member
No apology needed. I can't get it right the first, or even second, time.

"Simply put... "sort" is only useful in cases where your data exists entirely in the values of the map; the keys can have no meaning beyond simply being distinct."

"After the sort statement, the aggregate will have exactly the same sets of keys and values as before (even if the keys weren't consecutive), and the iteration order of the keys will be the same, but the values will likely be associated with different keys. "

That somehow seems to contradict your hope that they keys remain paired up.

So maybe some commentary about why you need two keys and what you are trying to do?

For the record, I tend to end up with something like

Code:
record thing {
    int key1;
    int key2;
    string namey;
}

thing [int] things

I don't care about the int used as index.

I'm also not sure what you indent with random. If you have a list and are trying to access it in a random order, I'm not sure you are actually getting what you want.

Continuing with the record, I might add a new field, ro, assign each record a random value for ro and the sort by ro.
 
It's a script that keeps track of my clanmates in a data file: basically playerID, name and an int that's when I last buffed them. The script runs through the clan roster, removing people or adding as necessary. I also keep a blacklist in case anyone violently disagrees with being buffed, in which case their lastBuff value is set to 999.

It then runs through the list, decreases the lastBuffed value, and if it's zero has a go at massaging feet, declaring employee of the month and so on. I like to do this last bit randomly, and currently have a rather clunky workaround that involves randomly sorting, then re-creating a new aggregate as I go through.

At the moment the script works, so that's fine.

However, there are a few bits and pieces that feel clunky, and everyone loves to make their code as efficient and sleek as possible. I realise I don't need to store playerID and playerName, for a start off, but when I began to fiddle with that I also realised I'd like to get this sorting business sorted, as it were.

I think, based on your helpful reply (thank you!), I might need to go with your records and use an int. Well, either that or just accept that my code looks terrible but at least it works!
 

fronobulax

Developer
Staff member
I understand the imperative :)

If I had three things and wanted to make a random choice I might stick them in an array, generate a random int between 0 and 2, inclusive, and use that as the index.

You might look at Crimbo2020FarmSpirit.ash (you may search to find it) which has an option for choosing a Crimbo spirit randomly.
 
Top