How to sort a list of strings on a substring

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Oct 5 22:35:14 EDT 2009


On Mon, 05 Oct 2009 15:45:58 -0700, n00m wrote:

> Here you are:
> 
> LogList = [\
>     "inbound tcp office 192.168.0.125 inside 10.1.0.91 88", "inbound tcp
>     office 192.168.0.220 inside 10.1.0.31 2967", "inbound udp lab
>     172.24.0.110 inside 10.1.0.6 161", "inbound udp office 192.168.0.220
>     inside 10.1.0.13 53"]
> 
> 
> LogList.sort(key=lambda x: x[x.index('1'):])


No, that's incorrect. Try it with this data and you will see it fails:


LogList = [
"inbound tcp office1 192.168.0.125 inside 10.1.0.91 88",
"inbound tcp office2 192.168.0.220 inside 10.1.0.31 2967",
"inbound udp lab1 172.24.0.110 inside 10.1.0.6 161",
"inbound udp office2 192.168.0.220 inside 10.1.0.13 53",
"inbound udp lab2 172.24.0.121 inside 10.1.0.6 161",
"inbound udp webby 220.96.0.2 inside 20.2.0.9 54",
]


Worse, if you delete the last item ("webby"), the code silently does the 
wrong thing. Code that crashes is bad, but code that silently does the 
wrong thing is a nightmare. Your test succeeded by accident -- it was a 
fluke of the data that you failed to see both failure modes.

The question asked was how to sort the list according to item 3 of the 
strings, *not* how to sort the list according to the first character '1'. 
The way to solve this correctly is by extracting item 3 and sorting on 
that, not by searching for the first character '1'. That is a hack[1] 
that just happened to work for the specific test data you tried it on.




[1] Hack in the bad sense, not in the good sense.



-- 
Steven



More information about the Python-list mailing list