<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>
conn = boto.connect_ec2()<br>
reservations = conn.get_all_instances()<br>
<br>
# this is where I'm trying to sort everything<br>
res_sorted = sorted(reservations, key=itemgetter('launch_time'))<br>
<br>
output = '<table>'<br>
output += '<tr>'<br>
output += '<th>State</th>'<br>
output += '<th>Launched</th>'<br>
output += '<th>Public Hostname</th>'<br>
output += '<th>Public IP</th>'<br>
output += '<th>Private Hostname</th>'<br>
output += '<th>Private IP</th>'<br>
output += '</tr>'<br>
for reservation in res_sorted:<br>
instance = reservation.instances[0]<br>
output += '<tr>'<br>
output += '<td>' + instance.state + '</td>'<br>
output += '<td>' + instance.launch_time +
'</td>'<br>
output += '<td>' + instance.public_dns_name +
'</td>'<br>
output += '<td>' + instance.ip_address + '</td>'<br>
output += '<td>' + instance.private_dns_name +
'</td>'<br>
output += '<td>' + instance.private_ip_address +
'</td>'<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>