# Applying a sort or filter to a dataset

**URL:** <https://forum.wixstudio.com/t/applying-a-sort-or-filter-to-a-dataset/33830>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [September 30, 2020, 10:28pm UTC](https://forum.wixstudio.com/t/applying-a-sort-or-filter-to-a-dataset/33830 "2020-09-30T22:28:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bspanger](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bspanger/32/11106_2.png) [@bspanger](https://forum.wixstudio.com/u/bspanger)\
**Post date:** [September 30, 2020, 10:28pm UTC](https://forum.wixstudio.com/t/applying-a-sort-or-filter-to-a-dataset/33830/1 "2020-09-30T22:28:56Z")

</div>

I have a handful of input elements linked to a dataset on my page, which is connected to a database table. I also have buttons to scroll through the data. This all works as expected. When I open the page the first record is displayed.  
What I’d like to do is allow the user to apply a sort. However, when I try to apply a sort from an event handler, such as this:  
export function sortButton\_click(event) {  
$w( “#ContractorsDataset” ).onReady(() =\> {  
$w( “#ContractorsDataset” ).setSort( wixData.sort()  
.ascending( “City” ))…  
then the display changes to show a different row, and I can’t figure out why it is showing the row it does. It doesn’t seem to be sorted, as I scroll through it. Calling refresh() doesn’t seem to help.

Additionally, I’d like to allow the user to apply a filter. For example,  
export function filterButton\_click(event) {  
$w( “#ContractorsDataset” ).onReady(() =\> {  
$w("# ContractorsDataset ").setFilter( wixData.filter().startsWith(‘City’, ‘A’))…  
The behavior here is even worse. All input elements are disabled, and when I query the count of dataset items after applying the filter, it is 0. Am I doing something wrong? Is this functionality not supported? Do I have to write the table / dataset interface and hand-roll the queries?

Any insight appreciated.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [September 30, 2020, 10:34pm UTC](https://forum.wixstudio.com/t/applying-a-sort-or-filter-to-a-dataset/33830/2 "2020-09-30T22:34:48Z")

</div>

Try " **c** ity" instead of " **C** ity".

---

<div class="post-metadata">

**Author:** ![bspanger](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bspanger/32/11106_2.png) [@bspanger](https://forum.wixstudio.com/u/bspanger)\
**Post date:** [September 30, 2020, 10:45pm UTC](https://forum.wixstudio.com/t/applying-a-sort-or-filter-to-a-dataset/33830/3 "2020-09-30T22:45:23Z")

</div>

J.D.,

Thank you for that quick and insightful response. Sure enough, I was confusing the Field Name with the Field Key. That did the trick on both counts. I wish I had asked earlier! Thanks again!
