Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20

Thread: The Future's So Bright, I Gotta Implement 'Sort'

  1. #1
    Developer jasonharper's Avatar
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    1,122

    Default The Future's So Bright, I Gotta Implement 'Sort'

    As of revision 6953, ASH has a 'sort' command for arrays & maps. It's always bothered me a bit that this couldn't be written in a completely generic manner in ASH itself - and it's quite possible that the near future will bring some additional things that you might want to sort.

    The syntax is:
    sort aggregate by keyExpr;
    aggregate is a reference to the object to be sorted - arrays are probably the most useful things to sort, but any mapping type can be used (even multidimensional maps, but note that you can only sort along a single dimension at a time). The reference must not be enclosed in parentheses, as that would look like a call to a function named sort() - which is still perfectly valid, "sort" has not become a reserved word.

    keyExpr is an arbitrary expression that defines how the items should be ordered. It is evaluated once for every entry in the aggregate, in a scope with two additional variables implicitly defined: 'index' and 'value', holding the details of that entry. The value of the keyExpr is used as the sort key; typically it would be an int or string, but can be any ASH type that can be compared via "<" and the other relational operators.

    The most basic form of sorting would therefore be "sort ... by value", but many useful things can be done with the use of a more complex keyExpr - the only real restriction is that the expression should not modify the object you're sorting. For example, if you had an array of items, you could sort it "by autosell_price(value)". An array of weapon items could be sorted "by -get_power(value)" to put it in decreasing order of power. If the elements of your aggregate are records, you'd need to use something like "by value.fieldName", since the records themselves can't be meaningfully compared.

    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. The sort is stable - in other words, elements with sort keys that compare as equal will remain in the same order. This means that you can sort on multiple criteria by simply performing separate sorts for each of the criteria, in increasing order of significance.

    A few more examples of things you can do:
    * "by -value" sorts integers in decreasing order (there's no similar trick for string values).
    * "by -index" reverses the existing order of an array (or map with integer keys).
    * "by random(1000000)" shuffles into a random order.
    * "by otherArray[index]" uses values from a parallel array as the sort keys (you'd then need to do "sort otherArray by value;" if you wanted the two arrays to remain in sync).
    Last edited by jasonharper; 02-14-2009 at 10:47 PM.
    In game: Seventh (#363053), originally a Seal clubber. I am the Ap'ostle of Ak'gyxoth! (Or is that Ap'ostrophe?)

  2. #2
    Minion Bale's Avatar
    Join Date
    Jun 2008
    Posts
    13,287

    Default

    item [int] options;
    int [item] maxval;
    sort options by maxval;

    Will that sort options by the associated integer value in the second map?

    eg,
    options [1] = $item[tonic water];
    options [2] = $item[$item[bottle of Monsieur Bubble]];
    options [3] = $item[Knob Goblin seltzer];
    maxval[$item[tonic water]] = 50;
    maxval[$item[bottle of Monsieur Bubble]] = 65;
    maxval[$item[Knob Goblin seltzer]] = 12

    Will become:
    options[1] == $item[Knob Goblin seltzer]
    options[2] == $item[tonic water]
    options[3] == $item[bottle of Monsieur Bubble]
    Last edited by Bale; 02-15-2009 at 12:08 AM.
    If people like my scripts, please send me stuffed Hodgmen.
    Universal Recovery, OCD Inventory Control, CounterChecker, newLife, ChIT.


  3. #3
    Developer jasonharper's Avatar
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    1,122

    Default

    That should be: sort options by maxval[value];
    There is pretty much always going to be a reference to 'value' (or less commonly, 'index') in the expression; without that, the entry's new position won't have anything to do with its current value, which is a fairly uncommon need.
    In game: Seventh (#363053), originally a Seal clubber. I am the Ap'ostle of Ak'gyxoth! (Or is that Ap'ostrophe?)

  4. #4
    Senior Member
    Join Date
    Sep 2008
    Posts
    795

    Default

    Holy moley this is supersweet. Just noticed it. Yay JH!

  5. #5
    Senior Member
    Join Date
    Mar 2009
    Posts
    650

    Default

    A bit of a necro here, but as there is no documentation of "sort" anywhere, other than here, and my question is a by-product of sorting, I'm asking here

    I have an array that I'm sorting by index. Some items in the array are capitalized and others are not. I'd like to sort ignoring case, is there an easy way to do that?

    Thanks,
    -Spiny

  6. #6
    Minion Bale's Avatar
    Join Date
    Jun 2008
    Posts
    13,287

    Default

    Not sure if this will work, but have you tried

    sort my_array by to_lower_case(index);
    If people like my scripts, please send me stuffed Hodgmen.
    Universal Recovery, OCD Inventory Control, CounterChecker, newLife, ChIT.


  7. #7
    Senior Member
    Join Date
    Mar 2009
    Posts
    650

    Default

    Not sure if this will work, but have you tried

    sort my_array by to_lower_case(index);
    Originally Posted by Bale View Post
    Thanks Bale,

    I was thinking about to_lower_case for this at the same time I was posting my message last night. I wasn't sure that I'd be able to use it, but it worked.

    I was using:
    Code:
    sort my_array by my_array[index];
    Your suggestion prompted me to change it to:
    Code:
    sort my_array by to_lower_case(my_array[index]);
    and that works

    Thanks!
    -Spiny

  8. #8
    Developer jasonharper's Avatar
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    1,122

    Default

    sort my_array by my_array[index];
    is more clearly and more efficiently expressed as:
    sort my_array by value;
    In game: Seventh (#363053), originally a Seal clubber. I am the Ap'ostle of Ak'gyxoth! (Or is that Ap'ostrophe?)

  9. #9
    Senior Member
    Join Date
    Mar 2009
    Posts
    650

    Default

    sort my_array by my_array[index];
    is more clearly and more efficiently expressed as:
    sort my_array by value;
    Originally Posted by jasonharper View Post
    Thanks

    Edit: Just ignore me I'm just confusing my own self lol.
    Last edited by Spiny; 09-18-2009 at 08:27 AM.

  10. #10
    Senior Member
    Join Date
    Sep 2008
    Posts
    795

    Default

    So I love sort. But I also really like maps (sort of, even though they often make my brain hurt). Could the language support something like this:

    foreach x in aggregate sort by value

    This would only affect the order in which the map values are extracted, and wouldn't affect the map itself.
    My scripts: Ascend for automatic ascensions, Farm to make more money than castlefarming, EatDrink for the maximum number of turns each day, LevelUp to gain a level safely, and PullCrap to manage your pulls in ronin.

Similar Threads

  1. Pork to the Future - Cyrus, Hyboria, Future, and Wumpus!
    By guyy in forum Turn-Burning Scripts
    Replies: 48
    Last Post: 11-07-2016, 06:01 AM
  2. Feature - Implemented Improve future guild compatibility
    By Bale in forum Bug Reports
    Replies: 2
    Last Post: 05-14-2013, 03:11 PM
  3. The future of Java and impacts on KoLmafia
    By wrldwzrd89 in forum Community Support
    Replies: 5
    Last Post: 04-16-2013, 08:20 PM
  4. Bug - Fixed The Future Quest - Completed without my consent
    By mstieler in forum Bug Reports
    Replies: 1
    Last Post: 10-05-2012, 05:31 PM
  5. suggestions for future versions
    By linguinelad in forum Scripting Discussion
    Replies: 3
    Last Post: 01-22-2011, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •