You may need to sort the elements of a collection by a certain value when performing certain actions.
In case there is no need in rearranging elements within a collection, you can use the following AnyLogic functions to create an ordered list and work with it in your code.
- List<T> sorted( Iterable<T> collection, ToDoubleFunction<T> value ) — Returns a new list with the elements or agents from the given collection rearranged and sorted in ascending order by a numeric value.
- List<T> sortedDescending( Iterable<T> collection, ToDoubleFunction<T> value ) — Returns a new list with the elements or agents from the given collection rearranged and sorted in descending order by a numeric value.
Examples:
List sortedByAgeAsc = sorted(people, p -> p.age);
List sortedByIncomeDesc = sortedDescending(people, p -> p.income);
The objects in your collection may implement the Comparable interface, which enables you to compare them to one another and apply natural sorting. Elements of the String, int, and double classes already have the Comparable interface implemented. To be able to sort elements of complex data types, you need to implement this interface yourself.
You can use the following functions for sorting elements within lists that contain comparable elements. All elements in these lists must implement the Comparable interface.
| Function | Description |
|---|---|
| void sortAscending( List<Comparable> list ) | Sorts (modifies) the specified original list in ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. The specified list must be modifiable. |
| void sortDescending( List<Comparable> list ) | Sorts (modifies) the specified original list in descending order, according to the inverted natural ordering of its elements. All elements in the list must implement the Comparable interface. The specified list must be modifiable. |
|
List<T> sorted( Iterable |
Returns a new list with the elements from the given collection rearranged and sorted in ascending order according to their natural ordering. All elements in the list must implement the Comparable interface. |
|
List<T> sortedDescending( Iterable |
Returns a new list with the elements from the given collection rearranged and sorted in descending order, that is, opposite to their natural ordering. All elements in the list must implement the Comparable interface. |
| List<T> sorted( Iterable<T> collection, ToDoubleFunction<T> value ) | Returns a new list with the elements or agents from the given collection rearranged and sorted in ascending order by a numeric value. |
| List<T> sortedDescending( Iterable<T> collection, ToDoubleFunction<T> value ) | Returns a new list with the elements or agents from the given collection rearranged and sorted in descending order by a numeric value. |
Example:
sortAscending( priceList );
Here, we sort the elements of the priceList list in ascending order, assuming that the list contains the values of the double type, which can be compared directly.
var newList = sortedDescending( people, p -> p.income );
This function returns a descending list of people, in the inverse order of their income.
-
How can we improve this article?
-