title: How do you sort a vector of custom objects by attribute, that is a Map of other custom objects?
body: <p>The hierarchy goes like:</p>
  
  <ol>
  <li><p>Class Drug - it has drugs name and price.</p></li>
  <li><p>Class Supplier - it holds a Map of <code>&lt;Drug drug, int quantity&gt;</code>.</p></li>
  </ol>
  
  <p>Now I have a <code>Vector&lt;Suppliers&gt;</code> (which holds, let's say 5 suppliers) and I want to sort in descending order of the quantity of an user inputted drugs name.</p>
  
  <p>Is this even possible? I've tried using comparators but I can't get it working.</p>
  
  <p>Ok, what I tried to do is put only the suppliers, that have the passed as parameter drug in a new <code>List</code> and THEN perform the sort on the list. I think this is a good workaround, but then I need to change the 'sort' somehow.</p>
  
  <p>Here is the method where I iterate over all suppliers and check if they have the drug, if they do -> add them in the new list and try to sort it in the end.</p>
  
  <pre><code>public List&lt;Supplier&gt; getSortedSuppliersByQuantity(Drug drug) {
      List &lt;Supplier&gt; sortedSuppliers = new ArrayList &lt;Supplier&gt;();
      for(Supplier s : suppliers) {
          for(Entry&lt;Drug, Integer&gt; entry : s.getListOfDrugs().entrySet()) {
              if(entry.getKey().getDrugsName().equals(drug.getDrugsName()));
                  sortedSuppliers.add(s);
          }
      }
      sort(drug, sortedSuppliers);
      return sortedSuppliers;
  }
  </code></pre>
  
  <p>Problem is that I get the <code>NullPointerException</code> at the <code>sort</code> line.</p>
  
  <p>I think that the <code>sort</code> method, that you suggested, needs to be changed to somehow act on the new List and not the old one?</p>
  
  <p>Stacktrace for @Andrew</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at java.util.Objects.requireNonNull(Unknown Source)
      at java.util.Optional.&lt;init&gt;(Unknown Source)
      at java.util.Optional.of(Unknown Source)
      at myapp.Supplier.getKeyExtractor(Supplier.java:22)
      at myapp.Orders.lambda$0(Orders.java:89)
      at java.util.Comparator.lambda$comparing$77a9974f$1(Unknown Source)
      at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
      at java.util.TimSort.sort(Unknown Source)
      at java.util.Arrays.sort(Unknown Source)
      at java.util.ArrayList.sort(Unknown Source)
      at java.util.Collections.sort(Unknown Source)
      at myapp.Orders.sort(Orders.java:89)
      at myapp.Orders.getSortedSuppliersByQuantity(Orders.java:106)
      at myapp.main.main(main.java:22)
  </code></pre>