Page MenuHomeFeedback Tracker

new command: sortBy (indirect sorting of mixed arrays with selection and evaluation)
Acknowledged, WishlistPublic

Description

We finally got a native sort function (Thanks guys! Much appreciated).

Unfortunately we can't sort an array of mixed arrays; a rather common use-case for custom data-structures of any kind. That's why we also need the command sortBy. Proposed syntax:

  <array> sortBy [<code>, <boolean>];

...where <array> is the array to be sorted, <code> is a selector, is given an element in <array> and returns the value that should be respected for the comparison. <boolean> indicates ascending (true) or descending (false) order.

Implementation details: in the worst case, you guys could implement an indirect sort for us, by building up a new array behind the scenes, based on <code> first (...just in case you would need a selector in a more formal/strict/deterministic/non-code way for a direct comparison sort).

Details

Legacy ID
2993467076
Severity
None
Resolution
Open
Reproducibility
N/A
Category
Feature Request
Additional Information

Example 1:

  _array sortBy [{ _this }, true];

...is equivalent to:

  _array sort true;

Example 2:

  _array = [
    ["a1", [1.5, 4.0]],
    ["a2", [0.1, 7.7]],
    ["a3", [1.0, 1.0]]
  ];
  
  _array sortBy [{ ((_this select 1) select 1) }, false];
  
  // -> [a2, a1, a3]

Example 3 (with same array from Example 2):

  _array sortBy [{(
    ((_this select 1) select 0) *
    ((_this select 1) select 1)
  )}, false];

  // -> [a1, a3, a2]

...which demonstrates not only "selection", but actually "evaluates" some new value based on <code>. Thus, with respect to a possible implementation, indirect sorting might be required anyways (unless "evaluation" is not allowed, and <code> can only express "selection").

Example 4:

_array = [<list of objects, such as units, buildings, locations, ...>]

_array sortBy [{ (player distance _this) }, true];

...which is also a neat use-case: sorting by things that are dynamic in nature, or things that you do not wanna keep in your (more) persistent data-structures/lists of things.

Alternative command name: indirectSort, or just overload the "sort" command. :)

Event Timeline

ruebe edited Additional Information. (Show Details)Apr 20 2015, 4:13 AM
ruebe set Category to Feature Request.
ruebe set Reproducibility to N/A.
ruebe set Severity to None.
ruebe set Resolution to Open.
ruebe set Legacy ID to 2993467076.May 8 2016, 11:58 AM

check example 4 https://community.bistudio.com/wiki/sort tonnes of custom sortings could be done with subarray sort.