D
Darko Aleksic
I tried finding out about this thing, but I just got more and more
frustrated... so - can someone just clarify this for me: if you give a
Comparator to a TreeMap, it must compare() keys, you can't compare
values?
What I've done before, when I needed a map sorted by values, was to
make a <Map.Entry<Key,Value>> Comparator, dump the entrySet() to a
list, sort it using that Comparator, clear the map and put everything
back in.
But I would like to be able to sort by keys or values when I wanted,
is it even doable? Or I have to create a new map every time? And if I
do, can't I just pass the old map and new Comparator (the one that
sorts by values or by keys, whatever is needed)?
And another thing - for normal sorting - if I change a field of a key
that is used while sorting, how do I tell the TreeMap that I've done
that? Or the only way is to remove the key, change that field and put
the key back in?
Hm, while typing this, I realized I might want to rethink what I'm
doing, but the questions still stand
frustrated... so - can someone just clarify this for me: if you give a
Comparator to a TreeMap, it must compare() keys, you can't compare
values?
What I've done before, when I needed a map sorted by values, was to
make a <Map.Entry<Key,Value>> Comparator, dump the entrySet() to a
list, sort it using that Comparator, clear the map and put everything
back in.
But I would like to be able to sort by keys or values when I wanted,
is it even doable? Or I have to create a new map every time? And if I
do, can't I just pass the old map and new Comparator (the one that
sorts by values or by keys, whatever is needed)?
And another thing - for normal sorting - if I change a field of a key
that is used while sorting, how do I tell the TreeMap that I've done
that? Or the only way is to remove the key, change that field and put
the key back in?
Hm, while typing this, I realized I might want to rethink what I'm
doing, but the questions still stand