Hacker News

Favorites Setup
Comment by hk__2 | original | HTML Can Do That
[−]hk__2 · 2026-08-20 Thu 15:45 UTC · link
Related to this, I’d love that HTML natively support sortable tables. This is a common need but every single time I have to reimplement it.
[−]krapp · 2026-08-20 Thu 16:10 UTC · link
That's what javascript is supposed to be for. All of this is what javascript is supposed to be for.

HTML describes layout, CSS describes style, JS adds interactivity.

Want sortable tables? Get a jquery plugin and spend five minutes, done.

This was all solved a decade or more ago.

[−]zephen · 2026-08-20 Thu 19:03 UTC · link
> HTML describes layout

News to me.

[−]krapp · 2026-08-20 Thu 19:13 UTC · link
That's understandable, because almost no one actually works with HTML directly anymore, but it's true. HTML contains tags like <H> for headers, <p> for paragraphs, <br> for line breaks <pre> for prerendered text, <ol> and <ul> for ordered and unordered lists, <table> for tables as well as <main>,<div>,<header>,<section>,<nav>,<article>,<aside>,<summary> and <footer> to both explicitly and implicitly describe the layout of an HTML document. In fact, "HTML" itself is an acronym (HyperText Markup Language) in which the "Markup" describes the function of HTML to "mark up" text in a similar way that editors once did in the print industry, describing which parts of the text should be bold, italicized, etc. or divided up and in what way.
[−]crab_galaxy · 2026-08-20 Thu 19:27 UTC · link
HTML describes the structure of a document, not really the layout.

If HTML had <inline> <block> <column> <row> or <grid> tags, sure. But CSS determines the layout via display/position properties.

[−]krapp · 2026-08-20 Thu 20:06 UTC · link
<span> is inline

<div> is block

<table>,<tr> and <td> are your grid, row and column.

CSS improves on describing layout with positioning but HTML was still doing that before CSS even came along.

[−]Zardoz84 · 2026-08-21 Fri 07:27 UTC · link
Suddenly it's 1999
[−]zephen · 2026-08-20 Thu 22:20 UTC · link
Thanks for the downvote, but you're wrong.

> explicitly and implicitly describe the layout of an HTML document.

No, this doesn't happen.

> In fact, "HTML" itself is an acronym (HyperText Markup Language)

Duh.

> in which the "Markup" describes the function of HTML to "mark up" text in a similar way that editors once did in the print industry, describing which parts of the text should be bold, italicized, etc. or divided up and in what way.

Which has fuck-all to do with the layout.

[−]hk__2 · 2026-08-20 Thu 19:46 UTC · link
> Want sortable tables? Get a jquery plugin and spend five minutes, done.

I want sortable tables without having to rely on a third-party plugin to a third-party library. HTML describes content, not just layout. It would be a `sortable` attribute on the table, just like you already have attributes that have nothing to do with layout such as `autocomplete` or `aria-*`.

[−]krapp · 2026-08-20 Thu 20:13 UTC · link
Everyone already complains that browsers are too bloated and now you want to have every browser support sortable tables when a single, simple JS file would work?
[−]ducktective · 2026-08-20 Thu 20:46 UTC · link
>single, simple JS file

It's all fine and dandy until marketing bros decide to shove all kinds of shady modals into that JS file. The JS side is not exclusive to trivial interactivity.

[−]krapp · 2026-08-20 Thu 20:57 UTC · link
What "marketing bros?"

It's open source software. You can see what it does, and you just put it on your server. We're talking about pre NPM javascript here, no continuous deployment, everything is vendored and local and no one is going to change that file without your knowledge and permission. And if someone does, you have much bigger problems on your hands.

What you can't own and control is the browser vendors and how they choose to implement things, or not to. You can edit a JS file to your specific needs, but you're stuck with whatever the browser decides.

[−]ducktective · 2026-08-21 Fri 02:25 UTC · link
My point was, people block js of websites by default. Why? Cause website devs abuse js and don't exclusively use it for basic interactivity of stuff like sorting table columns.

There is an incentive to use HTML for common interactivity purposes.

[−]ndriscoll · 2026-08-20 Thu 21:40 UTC · link
The overwhelming majority of web pages are static documents, and it's a document format, so basic document primitives make sense. Same with something like line charts or pie charts. Bloat is things like USB APIs that present obnoxious security surfaces.
[−]account42 · 2026-08-21 Fri 12:09 UTC · link
HTML has done interactivity before JS existed. No reason to not add support for sorting tables. JS is better suited to only handle non-standard cases where no native functionality exist as browser vendors can afford to spend infinitely more time to make sorting better than you can spend on doing it for one website.
[−]wild_egg · 2026-08-20 Thu 16:33 UTC · link
You want client side table sorting?

It's really easy to do this with server rendered HTML. Put a link with the sort param in the column headers and be done.

[−]mxuribe · 2026-08-20 Thu 16:55 UTC · link
I used to do this in the early 2000s! Some folks back then would ask whether i was afraid that clicking a link (which loads a "new" web page) would slow things down and create an awful experience for users...but, my team and I would really focus on keeping web pages slim/lightweight to begin with...so it rarely was a problem. I don't think we were geniuses or anything like that, but keeping things light (always and from the beginning) enabled us to "cheat" (like this "hack" of the links in the column headers) in ways that were beneficial but with very low risk. Sometimes it took an extra moment or two at the beginning of an effort...but it ALWAYS paid off down the road, and in many different ways.
[−]wild_egg · 2026-08-20 Thu 17:15 UTC · link
It still works exceptionally well in 2026 and generally creates better and faster experiences than an SPA with far, far less code.
[−]culi · 2026-08-20 Thu 17:38 UTC · link
I hate what it does to my back button
[−]hnal943 · 2026-08-20 Thu 17:48 UTC · link
HTMX is the solution
[−]zarzavat · 2026-08-20 Thu 18:36 UTC · link
So instead of using JS to sort the table directly, now you make a JS fetch() request to sort the table?!

It does make sense in one situation, if the table is a large server-side paginated one and the sorting is really an ORDER BY clause. But for a basic table that fits entirely on the client, it doesn't make sense at all. There are tiny JS libraries that will make <table>s sortable when you add a particular class name.

[−]everybodyknows · 2026-08-20 Thu 21:18 UTC · link
> tiny JS libraries that will make <table>s sortable

What are some good ones?

[−]nozzlegear · 2026-08-20 Thu 19:00 UTC · link
Trading one javascript for another?
[−]account42 · 2026-08-21 Fri 12:05 UTC · link
No to sorting tables where all the data is already available locally.
[−]hk__2 · 2026-08-20 Thu 19:44 UTC · link
You don’t need an SPA for that; Wikipedia has client-side sorted tables for example.
[−]xboxnolifes · 2026-08-20 Thu 17:46 UTC · link
I want tables that can be sorted on multiple columns without triggering a page refresh on every click.
[−]wild_egg · 2026-08-20 Thu 19:24 UTC · link
View Transitions are your friend
[−]account42 · 2026-08-21 Fri 12:06 UTC · link
It's a bit annoying when the sorting leaks into URLs people share, a bit like if the scroll position would be automatically included there.
[−]CrimsonRain · 2026-08-21 Fri 13:30 UTC · link
It's not as fast.

It is far more resources hungry when dealing with beyond 50 rows or more.

Puts needless pressure on backend.

It is not a good idea when sending whole dataset to client is feasible/better choice.

[−]hahn-kev · 2026-08-20 Thu 17:39 UTC · link
I feel like there's so few times it would actually work how I want it that it seems like there's little point.
[−]jwr · 2026-08-20 Thu 18:54 UTC · link
This is… not obvious. It's only easy if you want the simplistic approach of sorting lexicographically or numerically. But in more complex tables, you will often encounter cases where the data representation for sorting is different from data representation for display.

Of course most JavaScript-based tables get this wrong, too.

[−]hk__2 · 2026-08-20 Thu 19:41 UTC · link
You could have a `sortkey` attribute, a bit like <option> that has a `value` attribute in case the value you want is different from the value you display.
[−]donatj · 2026-08-21 Fri 11:56 UTC · link
Even then how does it interact with rowspans and colspans...
[−]dansalvato · 2026-08-20 Thu 20:39 UTC · link
I had always thought that table sorting seems like the perfect job for HTML, given its data-driven nature, and the possibility of implementing sort information into the attributes of table cell elements.

But thinking about it: Is there a precedent for HTML to be able to include information that instructs the device to present the DOM out of order? Maybe it's out of scope for HTML to have information about presentation order, when the order is supposed to be implied strictly by the DOM hierarchy itself.

Of course, CSS can visually reorder stuff (e.g. `order` on flex/grid items), but MDN has accessibility warnings regarding the use of `order` and visually presenting the data in an order not reflected by the DOM. So, maybe sorting is best done by reordering the DOM after all.

[−]ndriscoll · 2026-08-20 Thu 21:37 UTC · link
XSL has sorting, so kind of, depending on which document you're referring to when you say DOM.
[−]extra88 · 2026-08-21 Fri 00:33 UTC · link
CSS now also has `reading-flow` to instruct the browser to change the reading order to match the visual order created by Flex or Grid. Unfortunately, not all browsers support it and it has some limitations.
[−]cosmic_cheese · 2026-08-21 Fri 01:08 UTC · link
And related to that, native cell recycling for long lists would be nice, particularly with cases where the cells are on the heavy side.

Yes, it can be done with JS, but nothing is going to beat the browser engine for frequent DOM manipulation of that sort. It's also just one less dependency to have to pull in.