[dancer-users] Template Toolkit Sort Hash

WK wanradt at gmail.com
Sun Sep 27 13:13:12 BST 2015


2015-09-27 14:48 GMT+03:00 Kadir Beyazlı <kadirbeyazli at gmail.com>:

>> Of course, I can't be sure until I know what your data structure really looks like,
> [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
>
> $Pats = {ID => { SNAME => Value,
>                          CHNAME => Value,
>                          ANAME => Value,
>                      },
>             ID => { SNAME => Value,
>                          CHNAME => Value,
>                          ANAME => Value,
>                      },
>            ID => { SNAME => Value,
>                          CHNAME => Value,
>                          ANAME => Value,
>                      }
>             }

When we need those elements be in particular order, sorted by any
deeper value in hash tree, there is two main solutions for this goal.

First (and best for me). As hash can't maintain an order of elements,
you provide different data structure in right order, namely arrayref,
something like:

 $Pats = [ { 123 => { SNAME => Value,
                          CHNAME => Value,
                          ANAME => Value,
                      } },
             { 124 => { SNAME => Value,
                          CHNAME => Value,
                          ANAME => Value,
                      } },
            { 125 => { SNAME => Value,
                          CHNAME => Value,
                          ANAME => Value,
                      }
             } ];

Other variant is to use some helper in your templating system, which
provides map/reduce (Schwarzian transform) functionality. So you can
feed in your hash and it gives back id-s in specified order.

Hope it helps.

Wbr,
-- 
Kõike hääd,

G


More information about the dancer-users mailing list