I know in Java8
, it provided the below way to sort dictionary
. But how about dictionary in dictionary
? How can I pass the field
I wanna compare in .sorted()
? Or I should use another object type to hold the data? Thanks
Here's the sample dictionary and I wish to sort
by todayPrice
:
{
Apple={
todayPrice=10,
ytdClose=20,
Quantity=30.0
},
Orange={
todayPrice=3,
ytdClose=5,
Quantity=20.0
},
Cake={
todayPrice=87,
ytdClose=55,
Quantity=3.0
}
}
Sort one dictionary:
Stream<Map.Entry<K,V>> sorted =
map.entrySet().stream()
.sorted(Map.Entry.comparingByValue());