2
response = RestClient.get 'https://www.google.com/m8/feeds/contacts/default/full', {params: {'max-results': 10000, 'access_token' => access_token, 'alt' => 'json', 'sortorder' => 'ascending'}}

I am using google API to get contacts they have given sortorder in their documentation which can be ascending or descending, But I am not able to figure out they are sorting on which attribute, I would like to do that on name

Reference: https://developers.google.com/google-apps/contacts/v3/reference

enter image description here

Any help??

Edit: Also, I want to have pagination on contacts, If I handle this sorting on ROR side then I have to fetch thousand's of contacts every time and sort them and then paginate, which is slow

Vishal G
  • 1,521
  • 11
  • 30

2 Answers2

3

It seems that this Google API does not support sorting by name, which means pagination becomes a challenge.

If I was stuck on this, I would probably grab the full contacts list, and persist it in either
(a) a disk file, or
(b) Memcached or Redis, or
(c) a database table.
Of course other issues arise when persisting objects, the main one being defining good parameters for your code to know when to re-sync the list, or to simply throw it away.

If you choose (c), then your sorting and paging can be done the typical ways.

But if you choose (a) or (b), then you can sort at the time you save the list.

Ruby makes it easy to sort an array loaded into memory. If it's a big array, you can even sort it in place to avoid duplicating it. I'm not sure what your response object looks like, but here is some code assuming it's an array of objects with your name attribute.

my_objects = response # ...or whatever extracts the array of objects
# Modifies array in place...
my_objects.sort! { |a, b| a.name <=> b.name }
# To sort descending...
my_objects.sort! { |a, b| b.name <=> a.name }
# To sort by zip first (assuming a `zip` attr) then by `name`...
my_objects.sort! { |a, b| 2 * (a.zip <=> b.zip) + (a.name <=> b.name) }
# To sort by zip first (assuming a `zip` attr) then by `name` DESCENDING...
my_objects.sort! { |a, b| 2 * (a.zip <=> b.zip) + (b.name <=> a.name) }

This all works because of the signum or spaceship operator, <=>. You can learn more about it here:
What is the Ruby <=> (spaceship) operator?
https://en.wikipedia.org/wiki/Sign_function

Community
  • 1
  • 1
lilole
  • 322
  • 2
  • 9
  • @liole If user have 4000 contacts and I have to process on my side then it will take time, I am looking for google's support to solve this purpose – Vishal G Apr 11 '17 at 20:51
  • 1
    `sort_by!(&:name)` would be more idiomatic for ascending sort – engineersmnky Apr 11 '17 at 21:01
  • I have already achieved this on ROR side using sort_by, But I am looking for better approach........Right now I am getting thousands' of contacts and sorting it on my end and displaying .....Now I want to have pagination But every time I have to fetch these thousands of records again and again Then send paginated records only – Vishal G Apr 11 '17 at 21:18
  • as a secondary note for ascending `sort_by! { |a| [a.zip, a.name]}` will handle that too and more naturally. – engineersmnky Apr 12 '17 at 00:18
2

The Contacts API has no direct support for this.

The orderby param has only two options: "lastmodified" and just leaving it blank (which incidentally performs a deterministic-but-arbitrary order by using the contact ID's).

The reason for this is that generally the list action is only intended for sync purposes where the client/app will keep its own local copy that it periodically updates (as opposed to calling the API every time a user opens the app or changes views a different page of contacts). Furthermore, there are dozens of way you can sort a name (first name or last name as primary key? What about title? FileAs value? CJK considerations?)

Have you considered using the People API? It returns sort keys with the contacts for this purpose.

Blake O'Hare
  • 1,863
  • 12
  • 16
  • I have not considered people API because of `http://stackoverflow.com/questions/36386957/google-contacts-api-vs-people-api`, Is there any solid reason that I should use people API for this purpose?? I want to sort records on their name – Vishal G Apr 11 '17 at 21:21