Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added logical sorting alghorithm so a1,b1,a2,b2 will get sorted a1,a2,b1,b1 #30

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dist/flightkit-v0.0.11/flightkit.min.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,42 @@
return value.toLocaleLowerCase() === 'true'
}

function logicalSort(a, b, direction) {
const regex = /^([a-zA-Z]*)(\d*)|(\d*)([a-zA-Z]*)$/;

/** Extract the parts of the strings and make them case insensitive,
* else A1 and a1 will not allow multisort, which I think is confusing */
const matchA = a.toString().toLocaleLowerCase().match(regex);
const matchB = b.toString().toLocaleLowerCase().match(regex);

/** Determine the character and number parts */
const charPartA = matchA[1] || matchA[4];
const numPartA = parseInt(matchA[2] || matchA[3], 10) || 0;

const charPartB = matchB[1] || matchB[4];
const numPartB = parseInt(matchB[2] || matchB[3], 10) || 0;

/** check which order */
const desc = direction.includes('desc');

let leftHandCharValue = desc ? charPartB : charPartA;
let rightHandCharValue = desc ? charPartA : charPartB;
let leftHandNumValue = desc ? numPartB : numPartA;
let rightHandNumValue = desc ? numPartA : numPartB;

/** Compare the character parts first */
if (leftHandCharValue < rightHandCharValue) return -1;
if (leftHandCharValue > rightHandCharValue) return 1;

/* If characters are the same, compare the number parts **/
return leftHandNumValue - rightHandNumValue;
}

function sortFunction(applicableSorters, index = 0) {
return function (a, b) {
if (index > 0) {
debugger;
}

const { propertyName, direction } = applicableSorters[index];

Expand All @@ -74,13 +108,14 @@
const valueAHasNumber = extractNumber(valueA);
const valueBHasNumber = extractNumber(valueB);

let logicalSortNeeded = false;

if (valuesAreNumbers) {
valueA = parseFloat(valueA).toPrecision(12);
valueB = parseFloat(valueB).toPrecision(12);
}
else if (valueAHasNumber !== null && valueBHasNumber !== null) {
valueA = parseFloat(valueAHasNumber).toPrecision(12);
valueB = parseFloat(valueBHasNumber).toPrecision(12);
logicalSortNeeded = true;
}

if (valuesAreBooleans) {
Expand All @@ -105,10 +140,13 @@
}
}

// check if -1 or 1, 0. if 0 then check again.
/** check if -1 or 1, 0. if 0 then check again. */
let comparisonValue = 0;

if (valuesAreBooleans || valuesAreDates || valuesAreNumbers) {
if (logicalSortNeeded) {
comparisonValue = logicalSort(valueA, valueB, direction);
}
else if (valuesAreBooleans || valuesAreDates || valuesAreNumbers) {
/** Yes this works for all these things. :D */
comparisonValue = leftHandValue - rightHandValue;
}
Expand Down
1 change: 1 addition & 0 deletions dist/flightkit-v0.0.12/flightkit.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/cdn/ibiss-v0.0.12/avian.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/cdn/ibiss-v0.0.12/flightkit.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/cdn/ibiss-v0.0.12/htmx-ibiss-ui.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/cdn/ibiss-v0.0.12/rocket.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/js/flightkit.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions documentation/public/cdn/ibiss-v0.0.12/avian.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions documentation/public/cdn/ibiss-v0.0.12/flightkit.min.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions documentation/public/cdn/ibiss-v0.0.12/rocket.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion documentation/public/js/flightkit.min.js

Large diffs are not rendered by default.

46 changes: 42 additions & 4 deletions flightkit/flightkit-functions/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,42 @@ function getBooleanValue(value) {
return value.toLocaleLowerCase() === 'true'
}

function logicalSort(a, b, direction) {
const regex = /^([a-zA-Z]*)(\d*)|(\d*)([a-zA-Z]*)$/;

/** Extract the parts of the strings and make them case insensitive,
* else A1 and a1 will not allow multisort, which I think is confusing */
const matchA = a.toString().toLocaleLowerCase().match(regex);
const matchB = b.toString().toLocaleLowerCase().match(regex);

/** Determine the character and number parts */
const charPartA = matchA[1] || matchA[4];
const numPartA = parseInt(matchA[2] || matchA[3], 10) || 0;

const charPartB = matchB[1] || matchB[4];
const numPartB = parseInt(matchB[2] || matchB[3], 10) || 0;

/** check which order */
const desc = direction.includes('desc');

let leftHandCharValue = desc ? charPartB : charPartA;
let rightHandCharValue = desc ? charPartA : charPartB;
let leftHandNumValue = desc ? numPartB : numPartA;
let rightHandNumValue = desc ? numPartA : numPartB;

/** Compare the character parts first */
if (leftHandCharValue < rightHandCharValue) return -1;
if (leftHandCharValue > rightHandCharValue) return 1;

/* If characters are the same, compare the number parts **/
return leftHandNumValue - rightHandNumValue;
}

function sortFunction(applicableSorters, index = 0) {
return function (a, b) {
if (index > 0) {
debugger;
}

const { propertyName, direction } = applicableSorters[index];

Expand All @@ -72,13 +106,14 @@ function sortFunction(applicableSorters, index = 0) {
const valueAHasNumber = extractNumber(valueA)
const valueBHasNumber = extractNumber(valueB)

let logicalSortNeeded = false;

if (valuesAreNumbers) {
valueA = parseFloat(valueA).toPrecision(12);
valueB = parseFloat(valueB).toPrecision(12);
}
else if (valueAHasNumber !== null && valueBHasNumber !== null) {
valueA = parseFloat(valueAHasNumber).toPrecision(12);
valueB = parseFloat(valueBHasNumber).toPrecision(12);
logicalSortNeeded = true;
}

if (valuesAreBooleans) {
Expand All @@ -103,10 +138,13 @@ function sortFunction(applicableSorters, index = 0) {
}
}

// check if -1 or 1, 0. if 0 then check again.
/** check if -1 or 1, 0. if 0 then check again. */
let comparisonValue = 0;

if (valuesAreBooleans || valuesAreDates || valuesAreNumbers) {
if (logicalSortNeeded) {
comparisonValue = logicalSort(valueA, valueB, direction);
}
else if (valuesAreBooleans || valuesAreDates || valuesAreNumbers) {
/** Yes this works for all these things. :D */
comparisonValue = leftHandValue - rightHandValue;
}
Expand Down
39 changes: 34 additions & 5 deletions flightkit/public/js/databaseset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,48 @@ window.databaseSet = {
"column2": 2,
"column3": true,
"column4": null,
"column5": "value5"
"column5": "value5",
"column6": "a1"
},
{
"column1": "value6",
"column2": 7,
"column3": false,
"column4": "value8",
"column5": 9
"column5": 9,
"column6": "b2"
},
{
"column1": "value10",
"column2": 11,
"column3": true,
"column4": "value12",
"column5": 13
"column5": 13,
"column6": "A1"
},
{
"column1": "value14",
"column2": 15,
"column3": false,
"column4": "value16",
"column5": 17,
"column6": "b3"
},
{
"column1": "value18",
"column2": 19,
"column3": true,
"column4": "value20",
"column5": 21,
"column6": "a2"
},
{
"column1": "value18",
"column2": 19,
"column3": true,
"column4": "value20",
"column5": 21,
"column6": "b1"
}
],
"table2": [
Expand All @@ -29,14 +56,16 @@ window.databaseSet = {
"column2": 15,
"column3": false,
"column4": "value16",
"column5": 17
"column5": 17,
"column6": "b3"
},
{
"column1": "value18",
"column2": 19,
"column3": true,
"column4": "value20",
"column5": 21
"column5": 21,
"column6": "a2"
}
]
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@pennions/ibiss",
"version": "0.0.11",
"version": "0.0.12",
"avian_version": "0.0.2",
"flightkit_version": "0.0.11",
"flightkit_version": "0.0.12",
"htmx_plugin_version": "0.0.1",
"rocketjs_version": "0.0.1",
"description": "Frontend library of Pennions",
Expand Down