Skip to content

orderBy mutlitple parameters [fetaure request] #2518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dcrystalj opened this issue Mar 20, 2016 · 11 comments
Closed

orderBy mutlitple parameters [fetaure request] #2518

dcrystalj opened this issue Mar 20, 2016 · 11 comments

Comments

@dcrystalj
Copy link

it would be really nice to have option for sorting like this user in users | orderBy 'name' 'lastName'. if multiple users have same name they get sorted by lastname

@posva
Copy link
Member

posva commented Mar 20, 2016

Hi

Thanks for the feature request!
IMO the multi-field sorting is something usual and should be implemented. The impact on size is minimal and is not a breaking change
I'm implementing the feature asap tomorrow 😄

@rhyzx
Copy link

rhyzx commented Mar 22, 2016

@posva @yyx990803 how about supporting both key and custom sort fn like items | sortBy myComparator, it should be more flexiable in my opinion 😃

@posva
Copy link
Member

posva commented Mar 22, 2016

@rhyzx I'd like to but we need to add it as a last parameter in order to be safe from a breaking change 😢
We can also add it as an optional first parameter but it may become harder to understand (as a filter)

Here's what I've done so far: posva@3a55f3c
At the moment I'm breaking the grid e2e example and removing the ability to use the filter with null as a parameter, although imo there's no reason to call the filter manually with null
I'll try to fix it this evening

Edit: I didn't see the commit was already referenced 😮. I thought that didn't work with forks

@posva
Copy link
Member

posva commented Mar 25, 2016

@rhyzx About the comparator function I think this should be another filter called sort. orderBy orders by keys while sort should be simplier. I'm not sure if it should be added to the main repo

@dcrystalj
Copy link
Author

Thank you for not ignoring this request and all the effort you have put into it!
Passing custom sorting comparator like @rhyzx said makes sense as well. I hope both features will be included in the release one day.

@posva
Copy link
Member

posva commented Mar 29, 2016

@dcrystalj I appreciate you support 😄 . I've been a bit slow and I'm sorry, next time I'll handle a request faster 💪 . As for the custom comparator I think this should be included in another filter named sort because making the orderBy filter more complex isn't a good idea. Here is the code for the sort filter: https://jsfiddle.net/posva/5ky7ec1t. By default it compares by string though as that is the default behaviour for Array::sort. The code have been done in about 5 minutes 😅 and it can be improved.

@349989153
Copy link

It's very nice to have multiple fields to be sorted by the orderBy in the latest version.
Can we go further, I mean, multiple fields with multiple sort order like:
user in users | orderBy 'name' -1 'lastName' 1

Or maybe change the orderBy into orderBy chain:
user in users | orderBy 'name' -1 | orderBy 'lastName' 1

Thank you! Love vuejs!

@janbaykara
Copy link

@349989153 Any update on that suggestion? It'd be very welcome in a project I'm working on.

@posva
Copy link
Member

posva commented Jul 15, 2016

@janbaykara This may introduce a much more complicated filter for a very specific use case (tables)
I recommend you use a computed property and order yourself or using lodash:

      const sort = this.orders.reduce((sorting, {column, order}) => {
        sorting[0].push(column)
        sorting[1].push(order)
        return sorting
      }, [[], []])
      return _.orderBy(this.chapters, ...sort)

orders being an array specifying which properties shall be used to sort:

orders: [
        {
          column: '_id',
          order: 'desc'
        },
        {
          column: 'name',
          order: 'asc'
        }
      ]

This will lend to: _.orderBy(this.chapters, ['_id', 'name'], ['desc', 'asc'])

@RSamal
Copy link

RSamal commented Mar 1, 2017

return _.orderBy(this.chapters, ...sort) I am getting ReferenceError: _ is not defined
at VueComponent. Any suggestion I tried to do npm install underscore but that did not fix the issue.

@Akryum
Copy link
Member

Akryum commented Mar 1, 2017

@RSamal You need to include the library in the page or import it if you use a build tool:

import _ from 'underscore'

Or if you want to use lodash (recommended):

import _ from 'lodash'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants