# Sorting a table based on dropdown value

**URL:** <https://forum.wixstudio.com/t/sorting-a-table-based-on-dropdown-value/14860>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [November 5, 2018, 9:39pm UTC](https://forum.wixstudio.com/t/sorting-a-table-based-on-dropdown-value/14860 "2018-11-05T21:39:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alisonwinser](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alisonwinser/32/6846_2.png) [@alisonwinser](https://forum.wixstudio.com/u/alisonwinser)\
**Post date:** [November 5, 2018, 9:39pm UTC](https://forum.wixstudio.com/t/sorting-a-table-based-on-dropdown-value/14860/1 "2018-11-05T21:39:42Z")

</div>

Hi, I am trying sort a table of results and not having much luck.

So, I have a table of results that are created by looking up the records in my collection based on the output of a dropdown. So if I pick ‘New York’ in the drop down, all records with New York as the location are retrieved and displayed in a table. So far, so good.

But I need to also order the results in the table too - and that is where I am struggling.

It is slightly complicated because the ordering needs to differ based on the selection of the dropdown box. So, if the dropdown was ‘New York’, I want to go to a column called z\_nyc and sort by that column. If the dropdown was Los Angeles, I want to order the results by the column z\_la.

I am struggling to figure out how to do this. I thought maybe I need to query another collection to look up a string of the column name (i.e. z\_la), and use that as the ‘sort’. But after a few goes at that approach I’ve realised I really need to ask for help. I have no development background and this is day3 of wix coding for me so feel free to tell me this is totally the wrong approach!

So here goes - can anyone suggest an approach, and, ideally adjust my code to help?

Here is what I’ve done so far. There’s no error in the wix console, but I need to // out the ascending line or I get no table.

export function indexTownDropDown\_change(event, $w) {  
//Sets up the variable for the later sorting  
**var** mysortstring  
//Runs the query to populate the variable with the name of the column  
wixData.query(“Town\_Distance\_Lookup”)  
// Query the collection for any items whose “Name” field contains  
// the value the user selected in the dropdown  
// note there will only ever be one row that matches  
.contains(“Title”, $w(“#indexTownDropDown”).value)  
.find() // Run the query  
.then(res =\> {  
// Create an array object called items to be the results of the query  
//tempLookup is the column that holds the string of data that like z\_la that needs to become my sort order  
**let** items = res.items  
mysortstring = items.tempLookup  
})

// Now run my second query on another collection to  
// retrieve a subset of records and sort them by the looked up string variable  
wixData.query(“Activities”)  
// Query the collection for any items whose “Name” field contains  
// the value the user selected in the dropdown  
.contains(“location”, $w(“#indexTownDropDown”).value)  
.ascending(mysortstring)  
.find() // Run the query  
.then(res =\> {  
// Set the table data to be the results of the query  
$w(“#resultsTable1”).rows = res.items;  
$w(“#resultsTable1”).expand();  
});  
}

---

<div class="post-metadata">

**Author:** ![massasalah18](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/massasalah18/32/876_2.png) [@massasalah18](https://forum.wixstudio.com/u/massasalah18)\
**Post date:** [November 6, 2018, 9:25am UTC](https://forum.wixstudio.com/t/sorting-a-table-based-on-dropdown-value/14860/2 "2018-11-06T09:25:17Z")

</div>

Hello

make sure of the resulting value if it’s correct when querying Town\_Distance\_Lookup.  
can you provide your editor’s link for more help

Best  
Massa

---

<div class="post-metadata">

**Author:** ![alisonwinser](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alisonwinser/32/6846_2.png) [@alisonwinser](https://forum.wixstudio.com/u/alisonwinser)\
**Post date:** [November 6, 2018, 10:43am UTC](https://forum.wixstudio.com/t/sorting-a-table-based-on-dropdown-value/14860/3 "2018-11-06T10:43:39Z")

</div>

> **[Log In | Wix](https://users.wix.com/signin?originUrl=http%3A%2F%2Feditor.wix.com%2Fhtml%2Feditor%2Fweb%2Frenderer%2Fedit%2Fe707f630-a7a4-4f23-830c-780a2276e189%3FmetaSiteId%3D4611d5c2-a581-4c22-9252-e34b452bd97c%26editorSessionId%3Df885b2e6-5e4f-42a7-89ac-49e0a776735b%26referralInfo%3Ddashboard&redirectTo=http%3A%2F%2Feditor.wix.com%2Fhtml%2Feditor%2Fweb%2Frenderer%2Fedit%2Fe707f630-a7a4-4f23-830c-780a2276e189%3FmetaSiteId%3D4611d5c2-a581-4c22-9252-e34b452bd97c%26editorSessionId%3Df885b2e6-5e4f-42a7-89ac-49e0a776735b%26referralInfo%3Ddashboard&overrideLocale=en&forceRender=true)**
>
> Login to Wix.com

Code is on: Home\_version2\_with distance sort page

---

<div class="post-metadata">

**Author:** ![sapirh](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sapirh/32/6655_2.png) [@sapirh](https://forum.wixstudio.com/u/sapirh)\
**Post date:** [November 7, 2018, 2:46pm UTC](https://forum.wixstudio.com/t/sorting-a-table-based-on-dropdown-value/14860/4 "2018-11-07T14:46:11Z")

</div>

Hi Alison,

**Welcome to wix code!**  
I viewed your site and I have some suggestions:

**First** , pay attention to connect your table to the dataset in order to present the collection’s items at the table.  
You can read about how displaying database at [this](https://support.wix.com/en/article/wix-code-about-displaying-database-content-in-a-table-or-gallery) article.

**Second** , I suggest you to learn how to use the setFilter() method. read about that [here](https://www.wix.com/code/reference/wix-dataset.Dataset.html#setFilter) .  
This function can filter your data according to several fields and you can write it at the handler of the dropdown’s onChange() event instead of using the wixData.query method.

For more information regarding filtering options check out [this](https://www.wix.com/code/reference/wix-data.WixDataFilter.html) article.

I hope I helped.  
For more issues you can always contact us.

**Have a nice day and best of luck!**  
**Sapir,**
