Choose the two highest values from a list

You are in a closed Area with a variety of characters you can talk to. For asking and answering the right things you can build up relationships(->their “friendship” counter goes up). At the end of the Area I want that the two characters with the most “Friendship points” go to you and say goodbye. How can you make it that the game check's which of the characters have the highest value so that it prints the corresponding text based on the Outcome?


I believe there is a way to sort a list. (Yes, there is! http://docs.textadventures.co.uk/quest//functions/objectlistsort.html)

If you could sort by the attribute in question, then you could just pick off the two on the end.


ignore this post

my attempt at doing what the 'objectlistsort' Function already does, only to find out I need to have a sort function anyways within what I'm doing, and/or I need to use recursion+data_structure_design for this to be working correctly and/or to do the needed sorting...

<function name="get_character_max_value_function" parameters="objectlist_parameter, attribute_parameter, character_value_quantity_parameter" type="objectlist">
  not_handled_boolean_variable = true
  objectlist_variable = NewObjectList ()
  for (number_variable, 1, character_value_quantity_parameter) {
    list add (objectlist_variable, ObjectListItem (objectlist_parameter, number_variable - 1))
  }
  foreach (object_placeholder_variable_1, objectlist_parameter) {
    foreach (object_placeholder_variable_2, objectlist_variable) {
      if (not_handled_boolean_variable and object_placeholder_variable_1.attribute_parameter > object_placeholder_variable_2.attribute_parameter) {
        list remove (objectlist_variable, object_placeholder_variable_2)
        list add (objectlist_variable, object_placeholder_variable_1)
        not_handled_boolean_variable = false
        // I need to sort my list here, otherwise this won't work
        // (probably using recursion+data_structure design would make this much more clean/concise)
        // also, using a nested 'foreach' is 'N^2', very inefficient...
      }
      not_handled_boolean_variable = true
    }
  }
</function>

Just a note: if you wish to access an attribute indirectly (that is, via a string), then you can't just tack the variable onto an object. In other words, "object_placeholder_variable_1.attribute_parameter" will look for an attribute named "attribute_parameter", not what the variable "attribute_parameter" points to. You'd have to use GetAttribute to make it work as desired.

As far as the rest... Well, you posted it for some reason, I suppose. :)


was a failed attempt by me, more complicated than I thought, due to storing multiple most/min values... and adding/removing items from the list.


Yes, I got that!

If you wanted to try again, think about this (which might help):

  1. In Quest, you can only add items on the end of a list. So if you want to sort, you need to add them in sorted order.
  2. Start out with an empty result list
  3. While there are items left in the source list, find the item with the lowest attribute.
  4. Move it to the new list. (You had that idea in your code.)
  5. Repeat until no items left
  6. You can't modify lists that you're enumerating with foreach

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums