Custom Set Sorting
When you call insight.Scenario.getSet(), the data returned will be sorted either using default or specified custom sorting. The insight.addSetComparator(), insight.removeSetComparator() and insight.getSetComparator() methods are used to manage any custom Set sorting.
- a built-in comparator from the insight.enums.Comparators enums.
- a JavaScript array of ordered values or a custom comparator function.
- insight.enums.Comparators.BOOLEAN can cope with Sets of type Boolean, Integer, or String, and will try to cast the value into a Boolean. For example:
- true values could be t, True, 1, true.
- false values could be f, False, 0, false.
- insight.enums.Comparators.DAYS can be used to sort strings representing short or long names of days of the week in English, for example Mon, Tuesday.
- insight.enums.Comparators.MONTHS can be used to sort strings representing short or long names of months in English, for example Jan, December.
Example using a built-in comparator with a Set of strings
insight.addSetComparator('MonthsOfYear', insight.enums.Comparators.MONTHS);
Example using a JavaScript array of ordered values to sort a Set of strings
insight.addSetComparator('AlertLevels', ['Low', 'Medium', 'High', 'Critical'], insight.enums.SortDirection.DESC);
- less than 0 if a has a lower sorting value.
- 0 if a and b have equal sorting value.
- greater than 1 if a has a greater sorting value.
Example applying a custom comparator function on a Set of strings
This will sort the value A04 to the beginning of the data, and sort the rest of the values in alphabetical order:
insight.addSetComparator('AGENCY', function (a, b) { return a === 'A04' ? -1 : b === 'A04' ? 1 : a.localeCompare(b); });
Deprecated Set sorter API
Prior to JS API 4.7 the insight.addSetSorter() method was available. This has been deprecated in JS API 4.7, it will continue to work but may be removed in a later, major release. Instead you should replace any insight.addSetSorter() calls with insight.addSetComparator() and modify the function that performs the sorting to be a CompareFunction as described in the previous section. If both a set sorter and a set comparator are assigned to a given Set entity, the comparator will be applied and the set sorter will be ignored.