# Sort table in front end, any suggestion?

**URL:** <https://discourse.cakephp.org/t/sort-table-in-front-end-any-suggestion/10337>\
**Category:** Need Help\
**Created:** [April 26, 2022, 1:58am UTC](https://discourse.cakephp.org/t/sort-table-in-front-end-any-suggestion/10337 "2022-04-26T01:58:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigcouve](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@bigcouve](https://discourse.cakephp.org/u/bigcouve)\
**Post date:** [April 26, 2022, 1:58am UTC](https://discourse.cakephp.org/t/sort-table-in-front-end-any-suggestion/10337/1 "2022-04-26T01:58:14Z")

</div>

Hi everyone

Any suggestion about what can I use for sort a table generated in a foreach with cakephp 2? I know almost anything JavaScript, and I see that this can be an opportunity to practice it, but where do you recommend getting started?

A part of the table code:

```auto
<tbody>
            <?php
            foreach ($list as $post): ?>
                <tr id = "linePosts">
                    <?php if ($this->Session->read('logged') === true && $userRole === 'admin') { ?>
                        <td><?php echo $post['Post']['id']; ?></td>
                    <?php } ?>
                    <td>
                        <?php echo $this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id']));?>
                    </td>
                    <?php if (($this->Session->read('logged') === true) && ($userId === $post['Post']['user_id'] || $userRole === 'admin')) { ?>
                        <td>
                            <?php echo $this->Form->postLink(
                                'Deletar',
                                array('action' => 'delete', $post['Post']['id']),
                                array('confirm' => 'Você tem certeza?')
                            )?>
                            <?php echo $this->Html->link('Editar', array('action' => 'edit', $post['Post']['id']));?>
                        </td>
                    <?php }
                    else if (($this->Session->read('logged') === true)){ ?>
                        <td>-</td>
                    <?php } ?>    
                    <td><?php
                    $date = date_create($post['Post']['created']);
                    echo date_format($date, 'd/m/Y H:i:s'); ?></td>
                </tr>
            <?php endforeach;
            ?>
        </tbody>

```

---

<div class="post-metadata">

**Author:** ![chetanvarshney](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/chetanvarshney/32/2481_2.png) [@chetanvarshney](https://discourse.cakephp.org/u/chetanvarshney)\
**Post date:** [April 26, 2022, 2:21pm UTC](https://discourse.cakephp.org/t/sort-table-in-front-end-any-suggestion/10337/2 "2022-04-26T14:21:13Z")

</div>

You need to use any javascript sorting library to sort table data.

You can search on google or can visit this link [jQuery tablesorter 2.0](https://mottie.github.io/tablesorter/docs/)

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [April 26, 2022, 8:27pm UTC](https://discourse.cakephp.org/t/sort-table-in-front-end-any-suggestion/10337/3 "2022-04-26T20:27:34Z")

</div>

Another option is a server fetched partial.

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [April 27, 2022, 7:26pm UTC](https://discourse.cakephp.org/t/sort-table-in-front-end-any-suggestion/10337/4 "2022-04-27T19:26:17Z")

</div>

You can fetch your data to js variable (object, array), make sorting there and write function to render table from this data.
