kevin
(kevin)
April 25, 2018, 10:24pm
1
Is there a way to reverse sort a group so that the largest value appears first? Currently I have
gChargerTimes.members.sortBy[ (state as Number).intValue ].forEach [ NumberItem lowestRuntime | <do stuff> ]
which delivers the results from lowest to highest, and I need a way that delivers from highest to lowest
rlkoshak
(Rich Koshak)
April 25, 2018, 10:30pm
2
There are two ways:
gChargerTimers.members.sortBy[ -state ].forEach[ NumberItem lowestRuntime | <do stuff> ]
gChargerTimers.members.sortBy[ state ].reverseView.forEach[ NumberItem lowestRuntime | <do stuff> ]
The first one might need to be cast to a Number first:
gChargerTimers.members.sortBy[ -(state as Number) ].forEach[ NumberItem lowestRuntime | <do stuff> ]
There is no need to call intValue.
Reference: http://www.eclipse.org/xtend/documentation/102_moviesexample.html (Question 2)
kevin
(kevin)
April 25, 2018, 11:29pm
3
Thanks very much indeed @rlkoshak , I’ve been trying to google a solution for this one for some time now. I used your last one which is working well.
gChargerTimers.members.sortBy[ -(state as Number) ].forEach[ NumberItem lowestRuntime | <do stuff> ]
Interestingly in my original example if I remove “.intValue” the VB Code spits out
“Bounds mismatch: The type arguments <Item, Number> are not a valid substitute for the bounded type parameters <T, C extends Comparable<? super C>> of the method sortBy(Iterable, Function1<? super T, C>)”
rlkoshak
(Rich Koshak)
April 26, 2018, 2:27am
4
Not sure why it made that complaint. I’m 90% certain Number implements Comparable.
kevin
(kevin)
April 26, 2018, 2:48am
5
Either way the new setup is working and I’m very grateful for your help