Skip to content

Commit

Permalink
fix sorting of negative numeric values, fixes #392
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Aug 21, 2024
1 parent 6ba4196 commit f2c0c7d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
60 changes: 60 additions & 0 deletions docs/demos/28-sort-order-attribute/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="../../favicon.svg">
<title>Numeric sort - simple-datatables</title>
<!-- DataTable Styles -->
<link rel="stylesheet" href="../dist/css/style.css">
<!-- Demo Styles -->
<link rel="stylesheet" href="../demo.css">
</head>
<body>
<header>
<h1>
<a href="../../">simple-datatables</a>
</h1>
<a href="../../documentation">Documentation</a>
<a href="../">Demos</a>
</header>

<h2>Data-order attributes take precedence over cell values.</h2>
<p>Try sorting the two table columns.</p>
<table id="reports" class="table t-table-simple table-fix js-dataTable">
<thead>
<tr id="reports_Header">
<th>Name</th>
<th>Variation</th>
</tr>
</thead>
<tbody>
<tr>
<td data-order="B">A</td>
<td data-order="0">2000.9%</td>
</tr>
<tr>
<td data-order="A">B</td>
<td data-order="-0.5">100.3%</td>
</tr>
<tr>
<td data-order="D">C</td>
<td data-order="0.4">20%</td>
</tr>
<tr>
<td data-order="E">D</td>
<td data-order="-0.41">-7%</td>
</tr>
<tr>
<td data-order="C">E</td>
<td data-order="1.6">-9%</td>
</tr>
</tbody>
</table>
<script type="module">
import {DataTable} from "../dist/module.js"

window.dt = new DataTable("#reports", {})
</script>
</body>
</html>
1 change: 1 addition & 0 deletions docs/demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ <h2>Demos</h2>
<a href="25-cell-attributes/">Cell attributes</a>
<a href="26-numeric-sort/">Numeric sort</a>
<a href="27-load-json/">Load JSON</a>
<a href="28-sort-order-attribute/">Sort order attribute</a>
</nav>
</body>
</html>
2 changes: 1 addition & 1 deletion src/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export class Columns {
order1 = order2
order2 = temp
}
if (collator) {
if (collator && (typeof order1 !== "number") && (typeof order2 !== "number")) {
return collator.compare(String(order1), String(order2))
}
if (order1 < order2) {
Expand Down

0 comments on commit f2c0c7d

Please sign in to comment.