Skip to content
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

Sort by Multiple Columns #7

Open
dmerand opened this issue May 7, 2016 · 6 comments
Open

Sort by Multiple Columns #7

dmerand opened this issue May 7, 2016 · 6 comments

Comments

@dmerand
Copy link

dmerand commented May 7, 2016

Hi,

Looking at the API docs, it seems that Airtable should be able to support sorting via multiple columns via the API:

A list of sort objects that specifies how the records will be ordered. Each sort object must have a field key specifying the name of the field to sort on, and an optional direction key that is either "asc" or "desc". The default direction is "asc".

But I'm noticing that the records method (among others) in lib/airtable/table.rb seems to hard-code sort at one parameter:

    def records(options={})
      options["sortField"], options["sortDirection"] = options.delete(:sort) if options[:sort]
      results = self.class.get(worksheet_url, query: options).parsed_response
      RecordSet.new(results)
    end

I've tried simply commenting out the first line of that method and passing my own sort object (ie sort: [{field: 'fieldName', direction: :asc}], but likely due to my unfamiliarity with HTTParty or the Airtable API, I can't figure out how to pass multiple sort objects through the records method.

Basically: I would have submitted a pull request to adjust this method to allow multiple sort, but I can't figure it out :/ Hopefully you know more than me on the topic.

Thanks!

@syrnick
Copy link
Contributor

syrnick commented May 7, 2016

I haven't looked in the ruby code, but it seems that sort was an array with field and direction.

The sort should be json object like this:

[{"field": "Item", "direction": "desc"}]

@dmerand
Copy link
Author

dmerand commented May 7, 2016

Thanks, yes that appears to be the format for sorting by a single field. What I'm trying to figure out is how to sort by multiple fields.

For example, I want to sort a list of students by dormitory, then name, via the API. The best I've been able to come up with is to use a view in Airtable and set that view with a default multi-column sort. However that approach that doesn't work well when the data gets out-of-order (ie comes in randomly from a form) because I have to manually sort the form in Airtable before accessing it via my app. So I'd love to be able to sort multi-column directly via the API, I just can't figure out how to do it :/

@syrnick syrnick mentioned this issue Jun 8, 2016
@syrnick
Copy link
Contributor

syrnick commented Jun 8, 2016

@dmerand can you try sortBy branch to see if that works for you?

@dmerand
Copy link
Author

dmerand commented Jun 9, 2016

I tried the sortBy branch in my code, and it's working great! Thanks for taking the effort to make this update. It's very cool to be able to work directly on the returned object, rather than convert it to an array + sort it, which was my previous approach.

@Etsija
Copy link

Etsija commented Oct 20, 2016

...I think I was able to install the gem from GitHub from sortBy branch, but I cannot figure out how to use multiple keys. Have tried so far:

@rec = @tbl.records(:sort => [["key1", :asc], ["key2", :asc]])
but it doesn't work (returns nothing), while

@rec = @tbl.records(:sort => ["key1", :asc])
works as expected.

Could you give an example on how to use the multiple key sorting properly?

@syrnick
Copy link
Contributor

syrnick commented Oct 25, 2016

@Etsija try sortByTake2 branch - it now handles :asc being a symbol.

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

No branches or pull requests

3 participants