2

I know that Javascript object keys are not 'ordered' so by definition cannot be sorted – however I would like to output them in alphabetical order using Handlebars for display purposes.

The part of the object I'm interested in looks like this:

{
    "A publisher" : {
        journals : {
            "A journal" : {},
            "B journal" : {},
            "C journal" : {}
        }
    }

    "B publisher" : {
        journals : {
            "D journal" : {},
            "E journal" : {},
            "F journal" : {}
        }
    }
}

I'd like to be able to template the object using Handlebars ordered alphabetically by publisher, then each journal in alphabetical order within each publisher, something like:

{{#each this}}    //iterate over publishers in alphabetical order
    <h2>{{@key}}</h2>
    {{#each journals}}    //iterate over each journal in alphabetical order
    <ul>
        <li>{{@key}}</li> 
    </ul>
    {{//each}}
{{/each}}

I need to use a Handlebars helper for this I think, but am not really sure where to start. Can anyone point me in the right direction?

Thanks!

Huangism
  • 16,278
  • 7
  • 48
  • 74
ms813
  • 241
  • 3
  • 17

0 Answers0