Jump to content

Sort by existence of field


StanLindsey
 Share

Recommended Posts

I'm working on an e-commerce shop.

Currently, a categories default sorting is by descending price. 

The client doesn't have images for all products, wants the category sorted by products with images by default.

Easy:

sort=-image

This sorts by the filename, essentially alphabetically. 

I'd like to sort by those with images, with a secondary sort of price descending.

sort=-image, sort=price

This does not work, because the image sort is sorting alphabetically, not by the existence of the field. So you end up with this:

name     |     image    | price
product1 | product1.jpg | £12.34
product2 | product2.jpg | £43.43
product3 | product3.jpg | £12.20
product4 | product4.jpg | £76.20
product5 | product5.jpg | £12.50
product6 |     null     | £18.50
product7 |     null     | £16.21
product8 |     null     | £13.10

Notice that images prices are essentially ordered randomly, those with null image are ordered descending. 

So I need to have the selector sort by the existence of the image field, not by the content of the image field. 

I need to do this as part of the selector because some of these categories have 2000+ products and I cannot do it in-memory. 

Thoughts? 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...