title: Sorting by datetime with possible None values
body: <p>I am trying to sort based off a datetime field. There are instances where the field may be null ("None").</p>
  
  <p>This results in an error: </p>
  
  <blockquote>
    <p>TypeError: can't compare datetime.date to NoneType</p>
  </blockquote>
  
  <p>It looks like <a href="https://stackoverflow.com/questions/2178931/in-python-sorting-on-date-field-field-may-sometimes-be-null">this answer</a> would be the ideal way to fix the issue but it does not seem to work for me. Could someone explain what detail I seem to have overlooked? Im OK with Python (not great) and I understand what is supposed to happen here but feel there is some syntax issue I have overlooked.</p>
  
  <pre><code>mindate = datetime.date(datetime.MINYEAR,1,1)
  
  def getdepart_date(x):
      return x['depart_date'] or mindate
  
  requests = sorted(unsortedrequests, key=getdepart_date)
  </code></pre>
  
  <p>When I run it, I get the following error on the return line (with double underscores on either side of getitem):</p>
  
  <blockquote>
    <p>TypeError: 'StudTransReq' object has no attribute '__getitem_'</p>
  </blockquote>
  
  <p>Before None datetimes reared their ugly heads the code worked fine as follows:</p>
  
  <pre><code>requests = sorted(unsortedrequests, key=attrgetter('depart_date'))
  </code></pre>