Sort Html Links in One Line

by: George El., May 2019, Reading time: 2 minutes

Recently I had to sort a lot of html links in a program I created, so I decided to write a simple one line script to do it in python. The html links are in file called links.txt

The file looks like this:

<a href="file1">a</a>
<a href="file2">c</a>
<a href="file3">z</a>
<a href="file4">b</a>
<a href="file5">x</a>
<a href="file6">e</a>
<a href="file7">f</a>
<a href="file8">g</a>
<a href="file9">y</a>

the code I came up with is:

import re
print(*sorted([line for line in open("links.txt")],
                 key=lambda x:re.search(r'\>(.+)\<',x).group(1)))

Ok, I lied a bit, it is two lines, because I had to import re. Lets analyze this. We will use the sorted function in Python which returns a list and accepts as key a function that will return what needs to be sorted with. We use a lambda function that accepts x and returns what is between > and <. In this case, I am using a regular expression to find what is between > and < and store it in group(1) by using (.+). As first argument I need a list, which I read from the file using list comprehension. Finally I use the * keyword to print the elements of the list. The result is:

 <a href="file1">a</a>
 <a href="file4">b</a>
 <a href="file2">c</a>
 <a href="file6">e</a>
 <a href="file7">f</a>
 <a href="file8">g</a>
 <a href="file5">x</a>
 <a href="file9">y</a>
 <a href="file3">z</a>

I have also found in stackoverflow another solution, which is to use the split function as lambda. Here is how it would work:

print(*sorted([line for line in open("links.txt")],
                key=lambda x: x.split(">")[1].split("<")[0]))

this also yields the same result.

comments powered by Disqus