# Directory listing by weight

**URL:** <https://discourse.gohugo.io/t/directory-listing-by-weight/34057>\
**Category:** support\
**Created:** [August 1, 2021, 2:58pm UTC](https://discourse.gohugo.io/t/directory-listing-by-weight/34057 "2021-08-01T14:58:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prajkt\_Yeole](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/prajkt_yeole/32/13817_2.png) [@Prajkt\_Yeole](https://discourse.gohugo.io/u/Prajkt_Yeole)\
**Post date:** [August 1, 2021, 2:58pm UTC](https://discourse.gohugo.io/t/directory-listing-by-weight/34057/1 "2021-08-01T14:58:37Z")

</div>

[Split from [https://discourse.gohugo.io/t/list-all-pages-in-sub-folder/28063](https://discourse.gohugo.io/t/list-all-pages-in-sub-folder/28063)]

I have one query

```auto
{{ range (readDir "content/blog/") }}
  {{ if .IsDir }}
    <p>{{ .Name | title }}</p>

```

In the above code we are reading a directory can we filter the directory listing by weight? Since it’s a “Section” and it has \_index.md file.

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 1, 2021, 5:26pm UTC](https://discourse.gohugo.io/t/directory-listing-by-weight/34057/2 "2021-08-01T17:26:56Z")

</div>

Please provide more detail:

- Content structure
- Desired result

---

<div class="post-metadata">

**Author:** ![Prajkt\_Yeole](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/prajkt_yeole/32/13817_2.png) [@Prajkt\_Yeole](https://discourse.gohugo.io/u/Prajkt_Yeole)\
**Post date:** [August 2, 2021, 12:04pm UTC](https://discourse.gohugo.io/t/directory-listing-by-weight/34057/3 "2021-08-02T12:04:46Z")

</div>

sure will do  
folder structure -

```auto
|- content
  |- documentation
     |- Configuration
        |- _index.md
        |- file.md
     |- Getting Started
        |- _index.md
        |- fileA.md

```

my code -

```auto

   {{ range (readDir "content/documentation/") }}
      {{ if .IsDir }}
        <p>{{ .Name | title }}</p>
        <ul>
        {{ $path := printf "documentation/%s/" .Name }}
        {{ range where (where $.Site.RegularPages "Type" "config") "File.Dir" $path }}
        {{ $active := eq $.RelPermalink (.URL | relLangURL) }}
      {{ $active = or $active (eq $.Section (lower .Name)) }}
            <li><a {{ if $active }} class="active"{{ end }} href="{{ .RelPermalink }}">{{ .Title }}</a></li>
        {{ end }}
        </ul>
      {{ end }}
    {{ end }}

```

and using this code I’m trying to get all sections and inner files in a list view.

Now when I range through sections, it displays alphabetically. Can we range it by weight ??

I hope I clearly explained my query.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 2, 2021, 2:01pm UTC](https://discourse.gohugo.io/t/directory-listing-by-weight/34057/4 "2021-08-02T14:01:48Z")

</div>

With this structure:

```nohighlight
content/
├── documentation/
│ ├── configuration/
│ │ ├── conf-a.md
│ │ ├── conf-b.md
│ │ └── _index.md
│ ├── getting-started/
│ │ ├── gs-a.md
│ │ ├── gs-b.md
│ │ └── _index.md
│ └── _index.md
└── _index.md

```

I would do this:

```auto
{{ range (site.GetPage "documentation").Sections }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  <ul>
  {{ range .Pages}}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
  </ul>
{{ end }}

```

The default sort order is:

1. Weight (asc)
2. Date (desc)
3. LinkTitle (asc)
4. FilePath (asc)

Notes:

1. The template code above is not recursive. It will only list the immediate children of the documentation section.
2. When you assign weight, use positive or negative numbers, but not zero.

---

<div class="post-metadata">

**Author:** ![Prajkt\_Yeole](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/prajkt_yeole/32/13817_2.png) [@Prajkt\_Yeole](https://discourse.gohugo.io/u/Prajkt_Yeole)\
**Post date:** [August 2, 2021, 5:06pm UTC](https://discourse.gohugo.io/t/directory-listing-by-weight/34057/5 "2021-08-02T17:06:16Z")

</div>

works perfectly!! I think i complicated it.

Thank you so much @jmooring

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [August 4, 2021, 5:06pm UTC](https://discourse.gohugo.io/t/directory-listing-by-weight/34057/6 "2021-08-04T17:06:54Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
