Find the closest object (before and after) to the target object
<p>I want to Implement the two methods given below. <strong>SomeObject</strong> has a field <strong>createdDate</strong> of type Date</p>

<pre><code>private SomeObject getNearestObjectBeforeTargetObjectsCreatedDate(List&lt;SomeObject&gt; someObjectList, SomeObject targetObject){

}


private SomeObject getNearestObjectAfterTargetObjectsCreatedDate(List&lt;SomeObject&gt; someObjectList, SomeObject targetObject){

}
</code></pre>

<p>Suppose I have 5 objects P1, P2, P3, P4, P5 in ascending order of created dates. And <strong>target object is P3</strong>, then <strong>1st method should return P2</strong> and <strong>second should return P4</strong></p>

<p>Currently I have wirtten something like this</p>

<pre><code>private SomeObject getNearestPortFolio(List&lt;SomeObject&gt; someObjectList, SomeObject targetObject){
    SomeObject returnObject = targetObject;

      for(SomeObject someObject : someObjectList) {
        // if the current iteration's date is "before" the target date
          if(someObject.getCreatedDate().compareTo(targetObject.getCreatedDate()) &lt; 0) {

              if (someObject.getCreatedDate().compareTo(returnObject.getCreatedDate()) &gt; 0){
                  returnObject = someObject;
          }

      }
     }
      return returnObject;
    }
</code></pre>
