Sorting numbers with SI scale factors

Viewed 139

I have a CSV file that contains a column whose values are given with SI scale factors. I need to do a numeric sort on that column. Specifically, the CSV file contains a list of famous astronomical objects (the Messier objects) and I need to sort them by distance. The kicker is that the distance is given with numbers that use the SI unix prefixes, so a simple sort won't work. Is there a simple way of doing this?

Here is a very abbreviated version of the file:

"Messier Number","Distance"
"M1","6.5 kly"
"M2","33 kly"
"M7","980 ly"
"M16","7 kly"
"M19","29 kly"
"M31","2.5 Mly"
"M49","56 Mly"

Here is what I have so far::

from csv import DictReader

with open('m.csv') as f:
    messier = sorted(DictReader(f), key=lambda e: e['Distance'])

for entry in messier:
    print('{Messier Number:>5s} {Distance}'.format(**entry))

But this does an alphabetic sort rather than a numeric sort:

 M31 2.5 Mly
 M19 29 kly
  M2 33 kly
 M49 56 Mly
  M1 6.5 kly
 M16 7 kly
  M7 980 ly

I could try to split up the distance and interpret the k and M myself, but that seems like the wrong approach. After all, use of metric prefixes is very common. There must be some support for this already. Any pointers would be greatly appreciated.

2 Answers
Related