# Arranging Categories in order of number of Topics

**URL:** <https://meta.discourse.org/t/arranging-categories-in-order-of-number-of-topics/259549>\
**Category:** Feature\
**Created:** [March 27, 2023, 12:28pm UTC](https://meta.discourse.org/t/arranging-categories-in-order-of-number-of-topics/259549 "2023-03-27T12:28:22Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [March 28, 2023, 11:44am UTC](https://meta.discourse.org/t/arranging-categories-in-order-of-number-of-topics/259549/4 "2023-03-28T11:44:04Z")

</div>

No, no, for free 🙂.

EDIT:

You can install this script in a [theme component](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966#create-new-themes-and-theme-components-7).

It arranges the items of the categories list only on the page itself.  
This doesn’t affect any template or internal to keep better compatibility.  
Depending on your theme, it might require adjustments.

Let me know if it works for you.

> **js**
>
> ```js
> <script type="text/discourse-plugin" version="0.8">
> 
> api.onPageChange((url, title) => {
> const { defaultHomepage } = require('discourse/lib/utilities');
> 
> if (url !== '/categories' && (url !== '/' || defaultHomepage() !== 'categories')) {
> return;
> }
>     
> const desktop_category_page_style = api.container.lookup('service:site-settings').desktop_category_page_style;
>     
> const sort = ({parent, nodes, selector}) => {
> if (!parent || !nodes.length) {
> return
> }
> 
> Array.from(nodes).sort((a, b) => {
> const valueA = a.querySelector(selector).textContent;
> const valueB = b.querySelector(selector).textContent;
>             
> return valueB - valueA;
>             
> })
> .forEach(row => parent.appendChild(row));
> }
>     
> if (desktop_category_page_style.startsWith('categories')) {
> const parent = document.querySelector('.category-list tbody[aria-labelledby="categories-only-category"]');
>         
> sort({
> parent: parent, 
> nodes: parent?.querySelectorAll('tr'), 
> selector: 'td.topics span.value'
> })
> }
> else if (desktop_category_page_style === 'subcategories_with_featured_topics') {
> const childs = document.querySelectorAll('table.category-list');
>         
> sort({
> parent: childs[0]?.parentElement, 
> nodes: childs, 
> selector: 'th.parent-category span.value'
> })
> 
> } else {
> // boxes doesn't have a count displayed
> }
> });
> </script>
> 
> ```

---

_[View the full topic](https://meta.discourse.org/t/arranging-categories-in-order-of-number-of-topics/259549)._
