# Topic List Sort By Votes in voting plugin

**URL:** https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281
**Category:** Feature
**Tags:** topic-voting, completed
**Created:** [2017年五月24日 13:29 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281 "2017-05-24T13:29:56Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![kevinprow](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kevinprow/32/68511_2.png) [@kevinprow](https://meta.discourse.org/u/kevinprow)
#### Post date: [2017年五月24日 13:29 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/1 "2017-05-24T13:29:56Z")

</div>

We have a feature request category using the voting plugin, and the Votes sort order is available in our top menu, but is not available in the “Topic List Sort By:” dropdown in the category settings. I would love to set the default sort on this to show the highest votes at the top of the list. While I understand that sometimes a chronological view makes sense, in my use case, generating valuable feedback on the most requested features helps me to get a better picture of the benefit story for my customer base. Is there any hack I could do to make it default sort to Votes and/or is this something that is planned already for a future update?

 ![](https://global.discourse-cdn.com/meta/original/3X/b/5/b57fb07b8cca0e0f6a4b86342d2cd141eea246a6.png)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2017年五月24日 15:00 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/2 "2017-05-24T15:00:55Z")

</div>

Great suggestion, #pr-welcome

---

<div class="post-metadata">

### Author: ![adrianbblk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adrianbblk/32/87512_2.png) [@adrianbblk](https://meta.discourse.org/u/adrianbblk)
#### Post date: [2018年三月11日 09:29 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/3 "2018-03-11T09:29:19Z")

</div>

Hi,

Nothing new, since then?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [2018年三月11日 09:35 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/4 "2018-03-11T09:35:16Z")

</div>

Not that it’s 100% guaranteed, but if nobody has posted a “I’ll give it a try” it can be relatively safe to assume that the “pr\_welcome” is still open. Considering that it’s been 10 months, I say go for it.

---

<div class="post-metadata">

### Author: ![lookingsideways](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lookingsideways/32/120160_2.png) [@lookingsideways](https://meta.discourse.org/u/lookingsideways)
#### Post date: [2018年四月2日 12:42 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/5 "2018-04-02T12:42:25Z")

</div>

I’ve just had a quick look at this but it appears that the `Default Topic List` and `Topic List Sort By` dropdown options are hardcoded and not available to be extended by plugins.

What are the valid/desired approaches here?

1. Add a plugin-specific option to the hardcoded list in core
2. Add extension points that allow plugins to add options to default topic and topic list sort dropdowns

Option 1 would obviously be the quickest to implement but I have no idea if that approach would be accepted in core (/cc @sam).

Has option 2 been proposed before? If so are any specs available?

---

<div class="post-metadata">

### Author: ![lookingsideways](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lookingsideways/32/120160_2.png) [@lookingsideways](https://meta.discourse.org/u/lookingsideways)
#### Post date: [2018年四月2日 13:13 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/6 "2018-04-02T13:13:33Z")

</div>

Another potential option is allowing redirects for top-level category URLs. Eg:

`https://forum.ghost.org/c/Ideas`  
 →   
`https://forum.ghost.org/c/Ideas/l/votes`

That way the majority of links to the topic will end up on the “most-voted first” list whilst still allowing the “Latest” and “Top” menu items to work as before.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2018年四月3日 00:21 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/7 "2018-04-03T00:21:25Z")

</div>

In general I much prefer (2) a more extensible core opens the door to other plugins here which is desirable.

---

<div class="post-metadata">

### Author: ![Kameron\_Bourgeois](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@Kameron\_Bourgeois](https://meta.discourse.org/u/Kameron_Bourgeois)
#### Post date: [2018年七月27日 00:54 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/9 "2018-07-27T00:54:57Z")

</div>

Is there a solution yet to sort by number of votes?

---

<div class="post-metadata">

### Author: ![dan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dan/32/101549_2.png) [@dan](https://meta.discourse.org/u/dan)
#### Post date: [2018年七月28日 21:17 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/10 "2018-07-28T21:17:55Z")

</div>

If you have no problems in getting your hands a little dirty, you can edit `./app/assets/javascripts/discourse/components/edit-category-settings.js.es6` and add “votes” to the array described in function `availableSorts`, like so:

```javascript
  @computed
  availableSorts() {
    return [
      "likes",
      "op_likes",
      "views",
      "posts",
      "activity",
      "posters",
      "category",
      "created",
      "views",
    ]
      .map(s => ({ name: I18n.t("category.sort_options." + s), value: s }))
      .sort((a, b) => {
        return a.name > b.name;
      });
  },

```

Now, you should see `[[category.sort_options.voting]]` in settings.

---

<div class="post-metadata">

### Author: ![Thanatermesis](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thanatermesis/32/119521_2.png) [@Thanatermesis](https://meta.discourse.org/u/Thanatermesis)
#### Post date: [2018年七月28日 23:54 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/11 "2018-07-28T23:54:26Z")

</div>

can be this included in the plugin?

---

<div class="post-metadata">

### Author: ![dan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dan/32/101549_2.png) [@dan](https://meta.discourse.org/u/dan)
#### Post date: [2018年七月29日 08:49 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/12 "2018-07-29T08:49:00Z")

</div>

Currently, no. The plugin API does not offer this functionality yet. It will soon. 😉

---

<div class="post-metadata">

### Author: ![Scoobie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scoobie/32/122822_2.png) [@Scoobie](https://meta.discourse.org/u/Scoobie)
#### Post date: [2018年十月22日 05:05 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/13 "2018-10-22T05:05:27Z")

</div>

Is this sort by votes available yet? It’s a must have for me to use this plugin.

---

<div class="post-metadata">

### Author: ![mtan](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@mtan](https://meta.discourse.org/u/mtan)
#### Post date: [2018年十一月2日 06:04 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/14 "2018-11-02T06:04:34Z")

</div>

+1, looking for solutions similar to this one

---

<div class="post-metadata">

### Author: ![ariel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ariel/32/574107_2.png) [@ariel](https://meta.discourse.org/u/ariel)
#### Post date: [2018年十一月2日 06:45 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/15 "2018-11-02T06:45:00Z")

</div>

Here’s my +1 for this as well!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2018年十一月5日 16:00 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/16 "2018-11-05T16:00:05Z")

</div>

Could this be done in a theme?

Edit: (I infer that’s a “no.”)

How hard will it be to get this added to the theme? I have a client who really, really, really, wants this, but also has a really limited budget.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2018年十一月6日 01:12 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/17 "2018-11-06T01:12:03Z")

</div>

> [@pfaffman](#):
>
> Could this be done in a theme?

You can override computed properties in a theme by using `api.modifyClass`. Something like this should work. You’d need to test that the changes don’t break anything:

```plaintext
<script type="text/discourse-plugin" version="0.8.25">
  api.modifyClass('component:edit-category-settings', {
    availableSorts: Ember.computed(function() {
    let sorts = this._super();
    console.log('sorts', sorts);
    // Add a votes object to the sorts array. Then...
    return sorts;
    })
  });
</script>

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2018年十一月6日 02:15 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/18 "2018-11-06T02:15:12Z")

</div>

Thanks, Simon. I always appreciate your patience.

I add

```plaintext
<script type="text/discourse-plugin" version="0.8.25">
  console.log('something is sorting, batman!')
  api.modifyClass('component:edit-category-settings', {
    availableSorts: Ember.computed(function() {
    let sorts = this._super();
 
    console.log('sorts', sorts);
    // Add a votes object to the sorts array. Then...

    return sorts;
    })
  });
</script>

```

to `</head>`. I see something is sorting, batman!’ in the console log, but not `sorts`.

I think what’s supposed to happen is that I’d see what’s in the `sorts` array so that I could add stuff to it.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2018年十一月6日 02:26 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/19 "2018-11-06T02:26:00Z")

</div>

Try this, but make sure you test it:

```plaintext
 api.modifyClass('component:edit-category-settings', {
    availableSorts: Ember.computed(function() {
    let sorts = this._super();
    let voteSort = {};
    voteSort["name"] = "Votes";
    voteSort["value"] = "votes";
    sorts.push(voteSort);
    return sorts;
    })
  });

```

If you put a `console.log` statement in that function, you will only see its output when you open the category edit modal. You’ll also probably have to select ‘Preserve Log’ on your develper tools console to see anything.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2018年十一月6日 02:48 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/20 "2018-11-06T02:48:53Z")

</div>

> [@simon](#):
>
> If you put a `console.log` statement in that function, you will only see its output when you open the category edit modal.

Ah! Of course! As I said before, I really appreciate your patience. I can see that I’m getting closer, but don’t have time to finish it now.

Thanks again! I’ll update in my morning.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2018年十一月7日 15:46 UTC](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281/21 "2018-11-07T15:46:28Z")

</div>

Yup! That did it! Thanks so very much. And slowly, after fighting for over 20 years, I start to learn Javascript.

Thanks again, @simon. I really appreciate your help.

[下一頁](https://meta.discourse.org/t/topic-list-sort-by-votes-in-voting-plugin/63281.md?page=2)
