title: Which operations preserve RDD order?
body: <p>RDD has a <em>meaningful</em> (as opposed to some random order imposed by the storage model) order if it was processed by <a href="http://spark.apache.org/docs/latest/api/java/org/apache/spark/rdd/RDD.html#sortBy%28scala.Function1,%20boolean,%20int,%20scala.math.Ordering,%20scala.reflect.ClassTag%29" rel="noreferrer"><code>sortBy()</code></a>, as explained in this <a href="https://stackoverflow.com/a/29281548/850781">reply</a>.</p>
  
  <p>Now, which operations <strong>preserve</strong> that order?</p>
  
  <p>E.g., is it <strong><em>guaranteed</em></strong> that (after <code>a.sortBy()</code>)</p>
  
  <pre><code>a.map(f).zip(a) === 
  a.map(x =&gt; (f(x),x))
  </code></pre>
  
  <p>How about</p>
  
  <pre><code>a.filter(f).map(g) === 
  a.map(x =&gt; (x,g(x))).filter(f(_._1)).map(_._2)
  </code></pre>
  
  <p>what about</p>
  
  <pre><code>a.filter(f).flatMap(g) === 
  a.flatMap(x =&gt; g(x).map((x,_))).filter(f(_._1)).map(_._2)
  </code></pre>
  
  <p>Here "equality" <code>===</code> is understood as "functional equivalence", i.e., there is no way to distinguish the outcome using user-level operations (i.e., without reading logs &amp;c).</p>