improved sorting logic

This commit is contained in:
Martin McKeaveney 2020-07-28 19:40:52 +01:00
parent a62c8608c9
commit 7e80c1b4b9
2 changed files with 9 additions and 26 deletions

View File

@ -38,6 +38,7 @@
"gitHead": "eff4fa93ca1db11b97b5fdedc0c488413e277eb8", "gitHead": "eff4fa93ca1db11b97b5fdedc0c488413e277eb8",
"dependencies": { "dependencies": {
"@beyonk/svelte-googlemaps": "^2.2.0", "@beyonk/svelte-googlemaps": "^2.2.0",
"fast-sort": "^2.2.0",
"fusioncharts": "^3.15.1-sr.1", "fusioncharts": "^3.15.1-sr.1",
"svelte-fusioncharts": "^1.0.0" "svelte-fusioncharts": "^1.0.0"
} }

View File

@ -1,6 +1,7 @@
<script> <script>
import { onMount } from "svelte" import { onMount } from "svelte"
import { cssVars, createClasses } from "./cssVars" import { cssVars, createClasses } from "./cssVars"
import fsort from "fast-sort"
export let _bb export let _bb
export let onLoad export let onLoad
@ -23,28 +24,7 @@
} }
$: data = $store[model] || [] $: data = $store[model] || []
$: sorted = data $: sorted = sort.direction ? fsort(data)[sort.direction](sort.column) : data
$: {
let result = [...sorted]
if (!sort.column) sorted = result
console.log(sort);
if (sort.direction === "ASC") {
result.sort((a, b) => {
console.log(a[sort.column], b[sort.column])
return String(a[sort.column]).localeCompare(String(b[sort.column]))
}
)
}
if (sort.direction === "DESC") {
result.reverse()
}
sorted = result
}
$: if (model) fetchData() $: if (model) fetchData()
const shouldDisplayField = name => { const shouldDisplayField = name => {
@ -78,15 +58,15 @@
function sortColumn(column) { function sortColumn(column) {
if (column === sort.column) { if (column === sort.column) {
sort = { sort = {
direction: sort.direction === "ASC" ? "DESC" : null, direction: sort.direction === "asc" ? "desc" : null,
column: sort.direction === "ASC" ? sort.column : null, column: sort.direction === "asc" ? sort.column : null,
} }
return return
} }
sort = { sort = {
column, column,
direction: "ASC", direction: "asc",
} }
} }
@ -101,7 +81,9 @@
{#each headers as header} {#each headers as header}
<th on:click={() => sortColumn(header)}> <th on:click={() => sortColumn(header)}>
{header} {header}
{#if sort.column === header}v{/if} {#if sort.column === header}
{#if sort.direction === 'asc'}v{:else}^{/if}
{/if}
</th> </th>
{/each} {/each}
</tr> </tr>