<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    I'm using the Bono library for talking to EC2, and I'm getting a
    list of EC2 instances back in a list called "reservations". Each
    element in the list is a dictionary of information. One of those
    dictionary elements is a list called 'instances', and I only ever
    care about the first entry. That instances[0] value is a dictionary,
    which holds a key I want to use to sort the base "reservations"
    list.<br>
    <br>
    <font face="Arial">I found this helpful bit for sorting a list of
      dictionaries by a key within the dictionaries:<br>
    </font><a
href="http://stackoverflow.com/questions/72899/in-python-how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary">http://stackoverflow.com/questions/72899/in-python-how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary</a><br>
    <br>
    ... this works great if you just have a list of dictionaries. I'm
    not sure how to go deeper within that to sort my data without
    iterating through the entire 'reservations' list and building an
    entirely new list. Maybe it would be easier, but I'm just curious if
    it's possible without messing with my main 'reservations' list.<br>
    <br>
    <br>
    My current code looks like this: (it's a views.py file)<br>
    <br>
    from django.http import HttpResponse<br>
    import boto.ec2<br>
    from operator import itemgetter<br>
    <br>
    def index(request):<br>
    &nbsp;&nbsp;&nbsp; conn = boto.connect_ec2()<br>
    &nbsp;&nbsp;&nbsp; reservations = conn.get_all_instances()<br>
    <br>
    &nbsp;&nbsp;&nbsp; # this is where I'm trying to sort everything<br>
    &nbsp;&nbsp;&nbsp; res_sorted = sorted(reservations, key=itemgetter('launch_time'))<br>
    <br>
    &nbsp;&nbsp;&nbsp; output = '&lt;table&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;tr&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;th&gt;State&lt;/th&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;th&gt;Launched&lt;/th&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;th&gt;Public Hostname&lt;/th&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;th&gt;Public IP&lt;/th&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;th&gt;Private Hostname&lt;/th&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;th&gt;Private IP&lt;/th&gt;'<br>
    &nbsp;&nbsp;&nbsp; output += '&lt;/tr&gt;'<br>
    &nbsp;&nbsp;&nbsp; for reservation in res_sorted:<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; instance = reservation.instances[0]<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output += '&lt;tr&gt;'<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output += '&lt;td&gt;' + instance.state + '&lt;/td&gt;'<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output += '&lt;td&gt;' + instance.launch_time +
    '&lt;/td&gt;'<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output += '&lt;td&gt;' + instance.public_dns_name +
    '&lt;/td&gt;'<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output += '&lt;td&gt;' + instance.ip_address + '&lt;/td&gt;'<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output += '&lt;td&gt;' + instance.private_dns_name +
    '&lt;/td&gt;'<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output += '&lt;td&gt;' + instance.private_ip_address +
    '&lt;/td&gt;'<br>
    <br>
    <br>
    Ideally, I'd like to make each table column 'sortable' so the user
    can click on state/launched/etc (I may add more columns in the
    future), but I'm not sure how to search deeper within the
    'reservations' list for the sorted() call to get at the
    'launch_time' element within the instaces[0] dictionary.<br>
    <br>
    Also, I'm sure there are much better ways to do the display logic,
    but I'll tackle that another time.<br>
    <br>
    Thanks for any pointers,<br>
    Ian<br>
    <br>
  </body>
</html>