Sort dataset percentage values

I’ve created a function to sort a dataset based on user text input, where the value equals my dataset column header labels.

However the cell values of the column are numbers and currently they are sorting lexicographically.

I need to sort them numerically, but keep the user input based on text column header labels.

Any help is greatly appreciated, below is my code snippet:

export function sortButton_click(event) {
    let inputValue = $w("#sortInput").value.toLowerCase();
    $w("#dataset1").setSort(wixData.sort()
        .descending(inputValue)
    )
}

I don’t think I understood what you were trying to do.
Please elaborate and add examples.

Thanks for taking the time to reply.

  • Text user input searches for the column header.
  • Button sorts the dataset based on the text input.
  • The values in the columns are numbers.

The function works, however because it uses a text input it sorts lexicographically, instead of numerically.

I’ve made a quick and simple example, rather than my more complex site:
https://sod4mn.wixsite.com/demo

The only code used it the one shown above, hopefully this makes more sense? If not please let me know. Thanks once again.

Change the column type in the database collection to be Number.

@jonatandor35 Sorry - should of said, their field type is already set to number

@thesoddam so I don’t know. Maybe you change it to number on the sandbox collection and haven’t published it after that?

@jonatandor35 So I didn’t convert the numbers after changing field type, now the number columns work.

The problem remains for columns containing a % symbol (the majority of my original collection).

Converting them to numbers gets rid of the %

@thesoddam You should store them as numbers, and format them to text with % after query.
let say you store them as 5, 4, 35 etc…
Then you can try::

$w.onReady(() => {
$w("#dataset1").onReady(displayPercentage);
})
function displayPercentage(){
let rows  = $w('#table1').rows.map(e => {e.percent = e.percent + '%'; return e});
$w('#table1').rows = rows;
}
export function sortButton_click(event) {
    let inputValue = $w("#sortInput").value.toLowerCase();
    $w("#dataset1").setSort(wixData.sort()
        .descending(inputValue)
    )
.then(displayPercentage);
}

@jonatandor35 After trying to troubleshoot it myself, I can’t figure it out.

Formatting the ‘%’ works (i.e displayPercentage) - but with it included, the sort function doesn’t work.