How to sort option values

Hi, i build a filter for my table using the values in the database ({{ }}).
It shows up like How can i sort it ascending that i will see 1,2,3,4 ....?


One option is to sort the data from the query itself. Another is to create a transformer that gets the data from the query and sorts it. Then use the transformer for the select input instead of the query.

For example if you have the following SQL query:


select * from users

You can create the following transformer:


const usersData = {{}}
const usersArray = formatDataAsArray(usersData) // You don't need formatDataAsArray if your data already is in the format [{key: 'value'}, {key: 'value2'}]
return [...usersArray].sort((x, y) => x.first_name.localeCompare(y.first_name))

Then use the sortedUsers as a datasource for your select input.

1 Like

This almost works for me but not if there a null values.

How do we exclude null values with this transformer?

Hi @bg1900

Thanks for checking in on this! You should be able to add some JS to check for nulls before sorting. This external post has some ideas, as well as the below example:

If you'd prefer to just drop any records that have null values from your transformer, you could keep the code from @vangelov's answer and add a .filter:

const usersArray = formatDataAsArray(usersData).filter(x=>x.first_name)