# GroupBy for Data Files

**URL:** <https://discourse.gohugo.io/t/groupby-for-data-files/21412>\
**Category:** support\
**Created:** [October 24, 2019, 2:16am UTC](https://discourse.gohugo.io/t/groupby-for-data-files/21412 "2019-10-24T02:16:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FelicianoTech](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/felicianotech/32/22886_2.png) [@FelicianoTech](https://discourse.gohugo.io/u/FelicianoTech)\
**Post date:** [October 24, 2019, 2:16am UTC](https://discourse.gohugo.io/t/groupby-for-data-files/21412/1 "2019-10-24T02:16:20Z")

</div>

Hey,

So I want to be able to use [.GroupBy](https://gohugo.io/templates/lists#group-content) but for data files. It seems that `.GroupBy` is specific for pages. Any ideas on how I can do this?

**Context:**

I have a YAML data file full of locations. For each location, there’s a name, address, coordinates, phone number, etc. I want to list all locations out on a page, but group them by state.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [October 24, 2019, 5:52am UTC](https://discourse.gohugo.io/t/groupby-for-data-files/21412/2 "2019-10-24T05:52:46Z")

</div>

> [@FelicianoTech](#):
>
> It seems that `.GroupBy` is specific for pages.

To render groups of data files by a parameter contained in the data files themselves I use:

```auto
{{ $var := .Site.Data.<some-folder> }}
{{ range sort $var ".someParam" "desc"}}
...
{{ end }}

```

Obviously you can change the order by whatever you need by using i.e. `asc`

---

<div class="post-metadata">

**Author:** ![FelicianoTech](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/felicianotech/32/22886_2.png) [@FelicianoTech](https://discourse.gohugo.io/u/FelicianoTech)\
**Post date:** [October 24, 2019, 1:49pm UTC](https://discourse.gohugo.io/t/groupby-for-data-files/21412/3 "2019-10-24T13:49:07Z")

</div>

Thanks for that. Do you have a way to automate how `.someParam` is chosen?

If I used your example verbatim, I’d need to duplicate the `range` loop 53 times, one for each state and territory.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [October 24, 2019, 2:05pm UTC](https://discourse.gohugo.io/t/groupby-for-data-files/21412/4 "2019-10-24T14:05:19Z")

</div>

What is the actual form of these parameters? Are they nested inside a common parent parameter? Do these 53 parameters have anything in common?

If not I cannot see how you can render them in a terse way.

---

<div class="post-metadata">

**Author:** ![FelicianoTech](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/felicianotech/32/22886_2.png) [@FelicianoTech](https://discourse.gohugo.io/u/FelicianoTech)\
**Post date:** [October 24, 2019, 2:17pm UTC](https://discourse.gohugo.io/t/groupby-for-data-files/21412/5 "2019-10-24T14:17:06Z")

</div>

> [@alexandros](#):
>
> 53 parameters have anything in common

Yes the’re states.

Here’s an example of a data file I’d use:

```yaml
- name: "Queens Library Jamaica"
  phone: "(718) 555-0123"
  address:
    street1: "123-10 Hillside Avenue"
    city: "Richmond Hill"
    state: "NY"
    zip: "11418"
- name: "Queens Library Forest Hills"
  phone: "(718) 555-4567"
  address:
    street1: "153 Queens BLVD"
    city: "Forest Hills"
    state: "NY"
    zip: "11375"
- name: "Norfolk Public Library"
  phone: "(211) 555-0123"
  address:
    street1: "13 Navy Lane"
    city: "Norfolk"
    state: "VA"
    zip: "28567"
- name: "Clermont Library"
  phone: "(813) 555-0123"
  address:
    street1: "100 Heat Avenue"
    city: "Clermont"
    state: "FL"
    zip: "08109"

```

On the generated page, I want to be able to show each state, and then a list of locations in that state. Something like:

```auto
FL

Clermont Library - (813) 555-0123

VA

Norfolk Public Library - (211) 555-0123

NY

Queens Library Jamaica - (718) 555-0123
Queens Library Forest Hills - (718) 555-4567

```

If each location was a content/Markdown file, I could use `.GroupBy` and this would work perfectly. I’m trying to accomplish that same thing with a data file. Worst case scenario, I migrate the datafile to Markdown content files but there’s 100+ locations. Maintenance of this information will be much easier if it was in a single data file vs 100+ Markdown files with only a couple lines each.

---

<div class="post-metadata">

**Author:** ![FelicianoTech](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/felicianotech/32/22886_2.png) [@FelicianoTech](https://discourse.gohugo.io/u/FelicianoTech)\
**Post date:** [November 20, 2019, 1:48pm UTC](https://discourse.gohugo.io/t/groupby-for-data-files/21412/6 "2019-11-20T13:48:55Z")

</div>

I ended up figuring out a way to do this: [GroupBy For Data Files in Hugo](https://discourse.gohugo.io/t/groupby-for-data-files-in-hugo/21893)
