deitch/searchjs

Similar lib but for sorting?

lucasconstantino opened this issue · 7 comments

Does anyone know of a similar lib/system but for sorting?

@lucasconstantino what do you mean? Can you give a sample of what you are trying to do?

const people = [
  { name: 'Lucas', age: 26 },
  { name: 'Robert', age: 52 },
  { name: 'Avi', age: 26 },
]

const ordering = { age: 'DESC', name: 'ASC' }

SOMELIB.sort(people, ordering) // Original array in this order: Robert, Avi, Lucas

You could do it using native JS .sort(), but if you really want to do that with some lib to make it easier, lodash is your friend. https://lodash.com/docs/#orderBy and https://lodash.com/docs/#sortBy

Thing is I'm using your lib to allow sorting field results on GraphQL queries. Basically I'll allow the client app to filter/sort data results, and for that to work I need a form of static communication between the two. For sure I can implement a statically configurable sorting mechanism with Ramda, but I feel I'll be reinventing the wheel here. Mongo, for instance, has it's own JSON based "language" for search/filter/sort... I'm trying to find something similar, but adaptable for plain data - not database related.

You lost me a bit. Why does it matter if the results are sorted client or server side? searchjs, lodash, etc. all work on either end.

Unless you are saying you want to retrieve only a subset of the records - for 1,000 records, retrieve 101-150 - and which qualify as that subset depends on the sort order?

@deitch that's exactly the point. Getting the 5 eldest people would require a sorting previous to a limit, and well, if I have 1000 people for sure I would like to do that on the server side. Are you used to how GraphQL works? This kind of thing makes lots of sense when using GraphQL, and nowadays people usually create specific queries to handle these needs. But, if I have a common language between the two I can deliver much more power to the client.

@lucasconstantino yep, used graphql.

Is this not a question for your backend data store? Having it support sorting/ordering and filtering? Whenever I have done it, it always is a server-side implementation with participation of the data store.

Let us say you want to get the 5 oldest people. You could have your server ask your data store for all of the people and then sort them, then return the first 5. That is far better than sending all 1,000 (or 100,000) to the browser, but not as good as making the right query to your backend (RDBMS, NoSQL, whatever you like). Isn't it just a question of the right SQL query select ... order by ... or Couch view or whatever?