title: Create a dataframe based on 2 dataframes for eligibility and ordering
body: <p>I have 2 input dataframes like so</p>
  
  <p>eligibleDs</p>
  
  <pre><code>+---------+--------------------+
  |      cid|        eligibleUIds|
  +---------+--------------------+
  |     1234|offer3,offer1,offer2|
  |     2345|       offer1,offer3|
  |     3456|       offer2,offer3|
  |     4567|              offer2|
  |     5678|                null|
  +---------+--------------------+
  </code></pre>
  
  <p>suggestedDs</p>
  
  <pre><code>+---------+--------------------+
  |      cid|       suggestedUids|
  +---------+--------------------+
  |     1234|offer2,offer1,offer3|
  |     2345|offer1,offer2,offer3|
  |     3456|offer1,offer3,offer2|
  |     4567|offer3,offer1,offer2|
  |     5678|offer1,offer2,offer3|
  +---------+--------------------+
  </code></pre>
  
  <p>I want the output dataframe to be like so</p>
  
  <p>outputDs</p>
  
  <pre><code>+---------+--------+
  |      cid|  topUid|
  +---------+--------+
  |3456     |offer3  |
  |5678     |null    |
  |4567     |offer2  |
  |1234     |offer2  |
  |2345     |offer1  |
  +---------+--------+
  </code></pre>
  
  <p>The idea being that</p>
  
  <ul>
  <li>First data frame is a list of uids(each corresponding to some content id) that a user is eligible to see</li>
  <li>Second data frame is a suggested order of uids to be shown for that user</li>
  <li>If, for an id, the top suggested uid is present in the uids that can be shown then show that uid else move down the suggested list till you get to an uid that can be shown </li>
  <li>Basically eligibleDs decides the presence and suggestedDs decides the order</li>
  </ul>
  
  <p>I have been able to come up with something like this</p>
  
  <pre><code>val combinedDs = eligibleDs.join(suggestedDs, Seq("cid"), "left")
  val outputDs = combinedDs.map(row =&gt; {
    val cid = row.getInt(0)
    val eligibleUids = row.getString(1)
    val suggestedUids = row.getString(2)
    val suggestedUidsList = suggestedUids.split(",")
  
    var topUid = ""
    import scala.util.control.Breaks._
    breakable {
      for(uid &lt;- suggestedUidsList) {
        if(eligibleUids!=null &amp;&amp; eligibleUids.contains(uid)) {
          topOffer = uid
          break
        }
      }
    }
  
    Out(cid, topUid)
  })
  </code></pre>
  
  <p>This seems rather kludgy, can someone help let me know if there is a better way to do this?</p>