title: Java 8 Sort using 2 fields
body: <p>Based one some conditions, I have read data from MongoDB and creating a <code>List&lt;Document&gt;</code> with resultset.</p>
  
  <pre><code>List&lt;Document&gt; documentList = new ArrayList&lt;Document&gt;();
  </code></pre>
  
  <p>Sample Record Looks like:   </p>
  
  <pre><code>documentList: [
      Document{
          { _id=5975ff00a213745b5e1a8ed9,
              u_id=,
              visblty = 1,
              c_id=5975ff00a213745b5e1a8ed8,                
              batchid=null,
              pdate=Tue Jul 11 17:52:25 IST 2017, 
              locale=en_US,
              subject = "Document2"
          }     },
      Document{
          { _id=597608aba213742554f537a6,
              u_id=,
              visblty = 1,
              c_id=597608aba213742554f537a3, 
              batchid=null,
              pdate=Fri Jul 28 01:26:22 IST 2017,
              locale=en_US,
              subject = "Document2"
          }    } 
  ]
  </code></pre>
  
  <p>Using this documentList, again i am filtering using some conditions and then I need to sort the filter record based on some conditions (which I will get in request).</p>
  
  <pre><code>List&lt;Document&gt; outList = documentList.stream()
                  .filter(d -&gt; d.getInteger("visblty") == 1
                  &amp;&amp; (!StringUtils.isEmpty(req.pdate())? (d.getDate(CommonConstants.PDATE).after(afterDate)): true) 
                  &amp;&amp; (!StringUtils.isEmpty(req.pdate())? (d.getDate(CommonConstants.PDATE).before(beforeDate)): true)
                  .sorted().skip(4).limit()
                  .collect(Collectors.toList());
  </code></pre>
  
  <p>Not sure how to sorted (Dynamically needs to change the sorting order based on input, it looks like "<code>pdate by DESC</code>" or "<code>subject by ASC"</code>)</p>
  
  <p>Like: <code>"order by pdate DESC" or "order by pdate ASC"" or "order by subject DESC"</code></p>
  
  <p>How to sort using Comparator object of Document class.</p>
  
  <p>Note: I have tried couple of method which was suggested by the folks, but I didn't get any luck yet.
  Thank you in advance!</p>