# Allow User to sort data on site via "clicking" on column header

**URL:** <https://forum.wixstudio.com/t/allow-user-to-sort-data-on-site-via-clicking-on-column-header/61208>\
**Category:** Ask the community\
**Tags:** question, cms, code\
**Created:** [July 27, 2024, 7:40pm UTC](https://forum.wixstudio.com/t/allow-user-to-sort-data-on-site-via-clicking-on-column-header/61208 "2024-07-27T19:40:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Fowler](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/lee_fowler/32/87599_2.png) [@Lee\_Fowler](https://forum.wixstudio.com/u/Lee_Fowler)\
**Post date:** [July 27, 2024, 7:40pm UTC](https://forum.wixstudio.com/t/allow-user-to-sort-data-on-site-via-clicking-on-column-header/61208/1 "2024-07-27T19:40:08Z")

</div>

**Hello,**

**I am extremely new to this and have extremely limited coding experience. Most of my ocding is copying, pasting and editing the pertient bits to suit my needs.**

**I have so far figured out how CMS works to import tables into my site, and how to sort them as I wish to see them. However now i want to add the function of allowing site users to sort the data (not filter) on their chosen column.**

**So far I have added an invisible button above each column header I want them to be able to sort and I thought the code I entered below might work, however as I am here, you can imagine it didnt.**

* * *

import wixData from “wix-data”;

export function button1\_click(event) {

import wixData from “wix-data”;

$w(“#table1”).setSort(

wixData.sort().ascending(“K/D”).descending(“Name”),

## ); }

**As I said, totally new to this and coding isn’t something I’m overly experienced in.**

---

<div class="post-metadata">

**Author:** ![DeanAyalon](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/deanayalon/32/94633_2.png) [@DeanAyalon](https://forum.wixstudio.com/u/DeanAyalon)\
**Post date:** [July 27, 2024, 8:01pm UTC](https://forum.wixstudio.com/t/allow-user-to-sort-data-on-site-via-clicking-on-column-header/61208/2 "2024-07-27T20:01:50Z")

</div>

First, start by tagging your buttons so they’re not #button1, #button2, etc.

#titleHeader, #priceHeader for example (Whatever you feel is good to distinguish)

I don’t see a `setSort()` function available for $w.Table, did you see it documented somewhere?

In order to sort data, you need to connect your table to a dataset (anyone reading this, please correct me if I’m wrong)

After you set up a database collection, and connect a dataset to the table, you can use this function:

```js
    $w('#dataset').setSort(wixData.sort().ascending('fieldName'))

```

You can see a demo [here](https://yaron-ayalon.wixstudio.io/tests/table-sort), the sorting is made by selecting fields and not by clicking the headers, but you can use the code as reference
