I need a very quick and easy way to sort HTML tables. The table rows contain images that should stay with their appropriate row. I tried pasting my HTML into Libre Office calc, but the images are not pasted into rows, so sorting is not possible.

BTW, I do not want a sortable table. I want a sorted table. When done, I just want a plain HTML table that I can paste into a blog page, but I want the items in the table sorted.

I want to start with my clean HTML table, paste it into an app, sort the table and get the new HTML source without any added styling or junk having been added. It seems simple, but I can't find a solution.

Example of a table I wish to sort:

<meta content="text/html; charset=ISO-8859-1"
<table style="text-align: left; width: 100%;" border="1" cellpadding="2"
<td style="vertical-align: top;"> <a
href="http://example.com/images/a"> <img
src="http://example.com/images/a_thumb.jpeg" alt="image of a"> </a> </td>
<td style="vertical-align: top;">a<br>
<td style="vertical-align: top;"><a
href="http://example.com/images/f"> <img
src="http://example.com/images/f_thumb.jpeg" alt="image of f"> </a> </td>
<td style="vertical-align: top;">f<br>
<td style="vertical-align: top;"><a
href="http://example.com/images/c"> <img
src="http://example.com/images/c_thumb.jpeg" alt="image of c"> </a> </td>
<td style="vertical-align: top;">c<br>
<td style="vertical-align: top;"><a
href="http://example.com/images/b"> <img
src="http://example.com/images/b_thumb.jpeg" alt="image of b"> </a> </td>
<td style="vertical-align: top;">b<br>
  • How do you want to sort table? How is it structured? – cuonglm Aug 7 '14 at 3:47
  • 1
    I want to sort it alphanumerically in ascending order based on column 2 which contains item names. Column 1 is images. – MountainX Aug 7 '14 at 4:01
  • 1
    Can you give an example input? – cuonglm Aug 7 '14 at 4:02
  • 3
    @jasonwryan - I disagree. – MountainX Aug 7 '14 at 4:05
  • 1
    You said "If I were working on OS X or Windows I wouldn't even need to ask the question". What HTML editor would you use for this task in those OS? Maybe you could look up for a similar program, or ask for any replacement for Linux in the Software Recommendations site. – jimm-cl Aug 7 '14 at 5:16

You can use Ex editor (part of Vi/Vim) as demonstrated in the following shell command:

$ ex +"g/<tr/;,/tr>/join" +"/<table\_.\{-}\zs<tr/;,/table>/sort /.\{-}<a href/" +%p -scq! table.html | html2text 
[image of a]
[image of b]
[image of c]
[image of f]

Above example is using html2text command-line tool to display parsed HTML from stdin (install if required).

To save sorted table to the new file, replace +%p -scq! with +'wq! sorted.html', so:

ex +"g/<tr/;,/tr>/join" +"/<table\_.\{-}\zs<tr/;,/table>/sort /.\{-}<a href/" +'wq! sorted.html' table.html


  • +"cmd" - Executes Vim command.
  • g/<tr/;,/tr>/join - Joins lines between <tr/ and tr> (for easier sorting).
  • /<table\_.\{-}\zs<tr/;,/table>/ - Selects content between first <tr/ and /table>.
  • sort /.\{-}<a href/ - Sort above selection for lines starting after <a href/.
  • +%p - Prints buffer.
  • -scq! - Silently quit the editor without saving.

Check out similar example here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.