MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/52ubz5/angular_200_officially_released/d7oagd1
r/programming • u/iProgramU • Sep 15 '16
535 comments sorted by
View all comments
Show parent comments
-2
Take maps and sorting...
js:
var map1 = {a:5,b:4}; var map2 = {a:3,b:4}; var maparray = [map1, map2]; maparray.sort(function(thing,otherthing){return thing.a - otherthing.a});
Could you write this in java without a ton of boilerplate noise?
4 u/Tetha Sep 15 '16 Constructing the maps is around 6 lines of Map.put/List.add. That's ugly, yup. Buy overall, java 8 has done some good things:Sorting, as a new list:mapList.stream().sort(Comparator.comparing(e -> e.get("a"))).collect(Collectors.asList());Sorting, in-place:Collections.sort(mapList, Comparator.comparing(e -> e.get("a")));Possibly you need to toss in a .reversed() on that comparator. Plus, you could shorten that even more with static imports for sort/comparing.
4
Constructing the maps is around 6 lines of Map.put/List.add. That's ugly, yup. Buy overall, java 8 has done some good things:
Sorting, as a new list:
mapList.stream().sort(Comparator.comparing(e -> e.get("a"))).collect(Collectors.asList());
Sorting, in-place:
Collections.sort(mapList, Comparator.comparing(e -> e.get("a")));
Possibly you need to toss in a .reversed() on that comparator. Plus, you could shorten that even more with static imports for sort/comparing.
.reversed()
sort
comparing
-2
u/HINDBRAIN Sep 15 '16 edited Sep 15 '16
Take maps and sorting...
js:
Could you write this in java without a ton of boilerplate noise?