Custom sort with value?
See original GitHub issueDESCRIPTION
I’m looking to implement a custom sort that accepts a value, similar to GET /api/blogs?sort=count(articles) HTTP/1.1
like in the sorting example, but not sure I’m looking in the right place. Where would be a good place to look to implement something like this. I see examples with custom filtering, but I don’t see an example of a custom sort that accepts a parameter. Essentially I’m hoping to add a sort by distance feature that allows me to execute my code in Sql Server. Something like this:
// Distance parameters would be lat/long
GET /api/cities?sort=distance(27.0, -92.0)
// Somewhere in a ResourceDefinition, or Controller, or somewhere else?
var point = new Point(lat, long);
var results = queryable.OrderBy(m => m.Location.Distance(point));
Is there an exposed hook, or good place for me to look to get started on something like this?
VERSIONS USED
- JsonApiDotNetCore version: 4.0.0-rc
- ASP.NET Core version: 3.1
- Entity Framework Core version: 5.0
- Database provider: Sql Server
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Custom sorting in pandas dataframe - python
You can use this to create custom sorting functions. This works on the dataframe used in Andy Hayden's answer:
Read more >How to do a Custom Sort on Pandas DataFrame | by B. Chen
Pandas DataFrame has a built-in method sort_values() to sort values by the given variable(s). The method itself is fairly straightforward to use ...
Read more >Sort data using a custom list
In a column of a worksheet, type the values to sort by. · Select all of the cells in that list, and then...
Read more >Excel SORTBY function - custom sort with formula
The SORTBY function in Excel is designed to sort one range or array based on the values in another range or array. Sorting...
Read more >How To Perform Custom Sort In Google Sheets - YouTube
Unlike Excel, Google Sheets doesn't offer the custom sort feature. However, there still a few workarounds to perform a custom sort in Google ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It looks like you did everything right, based on my suggestions. JADNC adds sort by ID if none is provided, to prevent random orderings. It is unaware of your custom sort expression. EF Core probably understands that the sort by ID already makes the rows unique, so it discards any additional sort expressions (yours). The problem stems from here, where we first append the outcome of
OnRegisterQueryableHandlersForQueryStringParameters
to the EF Core query, before adding the outcome of query string parameters (including default sort if omitted). We do things in this order because usually devs implementOnRegisterQueryableHandlersForQueryStringParameters
to add extra filter conditions and we don’t want them to end up after sorting, projections etc. So my suggested approach does not work in your case.What you can try instead:
Next, you’ll need to register the custom repository from
Startup.ConfigureServices
(if you’re using auto-discovery, you can skip this step):The above results in your ordering being added after any existing orderings, which makes the earlier orderings irrelevant. It now depends on how smart EF Core is in eliminating unneeded sorts and reordering the query so that your where clause executes server-side. I hope this helps.
Another solution would be to implement
CitiesRepository.ApplyQueryLayer
like this:and keep using your resource definition with
OnRegisterQueryableHandlersForQueryStringParameters
override. The code above throws out any sort coming from query string orIResourceDefinition.OnApplySort
. The downside is you’ll get random ordering in case users do not specify yourdistance
query string parameter. But you can even work around that by only clearinglayer.Sort
if the query string parameter is provided (usingIHttpContextAccessor
check like above).There’s basically two ways to approach this: a generic solution and an endpoint-specific solution. The former is complex and involves a lot of work:
The latter (and easier) approach would be to implement
IResourceDefinition.OnRegisterQueryableHandlersForQueryStringParameters
. Using that you can bind a custom query string parameter to anIQueryable<T>
to extend the EF Core query with custom sort. Example:/stores?sort-by-location=27.0,-92.0