title: How to use Comparator with conditions in java
body: <p>I am trying to sort a list based on sort key and sort order I receive from an API.
  For example, 
  I have a list with sortkey and sortorder and based on that I need to sort.</p>
  
  <pre><code>List&lt;SortList&gt; sortlist;
  </code></pre>
  
  <p>I have a list of an object : </p>
  
  <pre><code>List&lt;Employee&gt; employee;
  </code></pre>
  
  <p>I am able to sort using </p>
  
  <pre><code>Collections.sort(sourceList, Comparator
                  .comparing(Employee::getAge).reversed()
                  .thenComparing(Employee::getCount));
  </code></pre>
  
  <p>But i need to check the sortfeild on a condition and based on that only the field is considered for sorting. </p>
  
  <p>ex:</p>
  
  <p>if(sortkey = "name")
  sortbythatkey from sortlist by the sort order</p>
  
  <p>if (sortkey = "place")
  sortbythat key from sortlist by the sort order</p>
  
  <p>So here if sortlist has both name and place then it should sort by both key and order</p>
  
  <p>Any idea how could i achieve this?</p>
  
  <p>Sort List contains:</p>
  
  <pre><code>{
      "sortKey":"name",
      "sortOrder":"ASC"
  
  },
  {
      "sortKey":"place",
      "sortOrder":"DESC"
  
  }
  </code></pre>
  
  <p>Requirement is to chain them together like ORDER BY in SQL</p>