title: How does mapreduce sort and shuffle work?
body: <p>I am using yelps MRJob library for achieving map-reduce functionality. I know that map reduce has an internal sort and shuffle algorithm which sorts the values on the basis of their keys. So if I have the following results after map phase</p>
  
  <pre><code>(1, 24) (4, 25) (3, 26)
  </code></pre>
  
  <p>I know the sort and shuffle phase will produce following output</p>
  
  <pre><code>(1, 24) (3, 26) (4, 25)
  </code></pre>
  
  <p>Which is as expected</p>
  
  <p>But if I have two similar keys and different values why does the sort and shuffle phase  sorts the data on the basis of first value that appears?</p>
  
  <p>For example
  If I have the following list of values from mapper</p>
  
  <pre><code>(2, &lt;25, 26&gt;) (1, &lt;24, 23&gt;) (1, &lt;23, 24&gt;) 
  </code></pre>
  
  <p>The expected output is</p>
  
  <pre><code>(1, &lt;24, 23&gt;) (1, &lt;23, 24&gt;) (2, &lt;25, 26&gt;)
  </code></pre>
  
  <p>But the output that I am getting is</p>
  
  <pre><code>(1, &lt;23, 24&gt;) (1, &lt;24, 23&gt;) (2, &lt;25, 26&gt;)
  </code></pre>
  
  <p>is this MRjob library specific? Is there anyway to stop this sorting on the basis of values??</p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>from mrjob.job import MRJob
  import math
  
  class SortMR(MRJob):
  
  
  
  def steps(self):
      return [
          self.mr(mapper=self.rangemr,
                  reducer=self.rangesort)]
  
  
  def rangemr(self, key, line):
      for a in line.split():
          yield 1,a
  
  
  def rangesort(self,numid,line):
      for a in line:
          yield(1, a)
  
  
  if __name__ == '__main__':
      SortMR.run()
  </code></pre>