Numeric column sorting disappeared in v3
Hello,
I've noticed that in v3 the ability to sort columns in report by numeric value has been removed. ("Numeric" class is no more detected and relevant key sorting function is removed). Can I restore this capability from v2? Or is there any reason it's not in v3 anymore?
Hey @lukjak thank you for filing this issue!. Could you attach a screenshot with what you're describing? It'll be easier to investigate
If I understand correctly, you're referring to 'numeric' class. That was removed in https://github.com/pytest-dev/pytest-html/commit/b57ca0bcf25e77bc95f1216eed51c7d235cb270e. The reason it was removed is because it was only used to sort values in the 'Duration' column. That column used to be reported as a double value of seconds+milliseconds but is now reported as a formatted time string ( see this link ).
All sorting works exactly as before for report columns with this change. Is there a bug you're seeing?
Hello
I observed it for a custom column (added "officially" via hooks) - so there is no issue in the default setup. It's used for test results ordering - so numbers are the most convenient.
Duration fails in v3x:
vvvDuration (highest first)
--
8.89
8.72
5.64
35.34
3.95
236.57
2.97
17.91
14.42
and the other way:
^^^Duration (lowest first)
--
14.42
17.91
2.97
236.57
3.95
35.34
5.64
8.72
8.89
You can patch this in main.js by adding a simple check for number and float and then subtract to find who is largest:
function numOrFloat(x) {
let regexPattern = /^-?[0-9\.]+$/;
if (regexPattern.test(x)) {
return true;
}
return false;
}
function sort(items, keyFunc, reversed) {
const sortArray = items.map(function(item, i) {
return [keyFunc(item), i];
});
sortArray.sort(function(a, b) {
const keyA = a[0];
const keyB = b[0];
if (keyA == keyB) return 0;
if (reversed) {
if (numOrFloat(keyA) && numOrFloat(keyB)) {
return (keyB-keyA)>0 ? 1 : -1;
}
return keyA < keyB ? 1 : -1;
} else {
if (numOrFloat(keyA) && numOrFloat(keyB)) {
return (keyA-keyB)>0 ? 1 : -1;
}
return keyA > keyB ? 1 : -1;
}
});
As a user, I also would expect that the Duration sort would be by the numeric value -- this makes it harder to seek tests of overly long duration.

I thought it was a great idea to have a column with individual test durations, so that I could sort by that column and see which tests were consuming most time and maybe needed to be optimised/tweaked. Until I found that sorting was not numerical...
I would assume (safely, I feel) that the consensus among users is that the sort here should clearly be numeric and not as a string (time-specific or otherwise). The value itself (either in fact or in visual representation) is not a time value. If anything, it would be a timedelta which would sort properly.
If PR #464 solves the issue, that would be fantastic. Is anyone looking into its efficacy?
I agree, will make sure to correct this in the next major release.
Thank you!