title: Why printing inside foreach doesn't reflect an order of elements
body: <p>May be I am missing something but I expected the data to be sorted based on the key</p>
  
  <pre><code>scala&gt; val x=sc.parallelize(Array( "cat", "ant", "1"))
  x: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[160] at parallelize at &lt;console&gt;:22
  
  scala&gt; val xxx=x.map(v=&gt; (v,v.length))
  xxx: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[161] at map at &lt;console&gt;:26
  
  scala&gt; xxx.sortByKey().foreach(println)
  (1,1)
  (cat,3)
  (ant,3)
  
  scala&gt; xxx.sortByKey().foreach(println)
  (cat,3)
  (1,1)
  (ant,3)
  </code></pre>
  
  <p>It works if I tell spark to use only 1 partitions as below but how to make this work in a <strong>cluster or more than 1 workers</strong>?</p>
  
  <pre><code>scala&gt; xxx.sortByKey(numPartitions=1).foreach(println)
  (1,1)
  (ant,3)
  (cat,3)
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>I think I got the answer. It is being sorted correctly as it works when I use the collect</p>
  
  <pre><code>scala&gt; xxx.sortByKey().collect
  res170: Array[(String, Int)] = Array((1,1), (ant,3), (cat,3))
  </code></pre>
  
  <p>Keeping the question open to validate my understanding.</p>