title: How can I sort a Map by some property of the key in the map?
body: <p>Lets say we have a class called Activity:</p>
  
  <pre><code>public class Activity {
      private Project project;
  }
  </code></pre>
  
  <p>So every Activity Object will have a Project.. Also every Project has a Unit. Two different Projects can have the same unit:</p>
  
  <pre><code>public class Project {
      private String projectName; 
      private Unit unit;
  }
  </code></pre>
  
  <p>And a Unit has a name as well:</p>
  
  <pre><code>public class Unit {
      private String unitName;
  }
  </code></pre>
  
  <p>So currently I have a TreeMap like this:</p>
  
  <pre><code>SortedMap&lt;Project,List&lt;Activity&gt; myMap
  </code></pre>
  
  <p>One example may be:</p>
  
  <pre><code>projectOne -&gt; [activityOne,activityTwo,activityThree]
  projectTwo -&gt; [activityThree,activityFouractivityFive]
  projectThree -&gt; [activityFour,activityFive,activitySix]
  </code></pre>
  
  <p>etc...</p>
  
  <p>Now lets assume projectOne has the same unit with projectThree (unitAAA) and projectTwo has unitZZZ...  </p>
  
  <p>I want to sort my Map alphabetically by the unit of the project elements:  </p>
  
  <pre><code>projectOne -&gt; [...]
  projectThree -&gt; [...] 
  projectTwo -&gt; [...]
  </code></pre>
  
  <p>How can I achieve this? I know in Stackoverflow the question is <strong>What have you tried so far?</strong>, well I am really stuck in this point so I have not really tried anything besides trying to come up with what I can even try.. </p>