title: Sorting data by class for each date
body: <p>I have some datasets which I need to be able to group by <code>uniquenameclass</code> for each date. In my example data below I have used <code>UniqueNameClass1</code>, <code>UniqueNameClass3</code> etc, however in reality they will be text strings only. So what I need to be able to do is cycle through the csv selecting a group which is all the same date then, then group within this date group by the <code>UniqueNameClass</code>. If this is difficult to understand hopefully this example will shed some light on the problem I have:</p>
  <h3>raw data</h3>
  <pre><code>text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass1,text,text 
  text,text,text,13/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass2,text,text
  </code></pre>
  <h3>data sorted by date and then grouped by UniqueNameClass</h3>
  <pre><code>text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,11/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,12/03/12,text,text,text,UniqueNameClass5,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass1,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass2,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass3,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass4,text,text
  text,text,text,13/03/12,text,text,text,UniqueNameClass5,text,text
  </code></pre>
  <p>So only data of the same date can be grouped together by <code>UniqueNameClass</code>. I know in this example I have put the groups chronologically in reality it doesn't matter on the order of the groups, just that each row from each groups on a given date are next to each other.</p>
  <h2>Edit</h2>
  <p>I have attempted to run the code supplied by phihag, quite a few different ways however everything I have tried has yielded the traceback:</p>
  <pre><code>File &quot;C:\RawDataeDataTest.py&quot;, line 6, in &lt;module&gt;
      data = list(csv.reader(io.StringIO('RawDataeDataTest.csv')))
  TypeError: initial_value must be unicode or None, not str
  </code></pre>
  <p>The code that I thought would work in accordance with both the code supplied and the comments provided were:</p>
  <pre><code>import csv
  import io
  
  data = list(csv.reader(io.StringIO('RawDataeDataTest.csv')))
  data.sort(key=lambda row: (row[3], row[7]))
  print(u'\n'.join(u','.join(row) for row in data))
  </code></pre>
  <p>and</p>
  <pre><code>import csv
  import io
  
  data = list(csv.reader('RawDataeDataTest.csv'))
  data.sort(key=lambda row: (row[3], row[7]))
  print(u'\n'.join(u','.join(row) for row in data))
  </code></pre>
  <p>For the latter I actually get a different traceback:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\RawDataeDataTest.py&quot;, line 7, in &lt;module&gt;
      data.sort(key=lambda row: (row[3], row[7]))
    File &quot;C:\RawDataeDataTest.py&quot;, line 7, in &lt;lambda&gt;
      data.sort(key=lambda row: (row[3], row[7]))
  IndexError: list index out of range
  </code></pre>