Using Sort inside query parameters

In the present article, we will see how it is possible to define in which order and based on which criteria  the images in the search results should appear. 

We will make use of an Apex Class Controller and a canvasApp on a Visualforce Page so as to define the sort order inside the query parameters. 

Apex Controller

The code snippet below shows the implementation of the Apex Class Controller.

public class SharinPixSearchController {
  public Map<String, Object> params;
  private sharinpix.Client clientInstance = sharinpix.Client.getInstance();

  public SharinPixSearchController(ApexPages.StandardController controller) {
    Map<String, Object> query = new Map<String, Object> {
      'q' =>  '*',
      'abilities' => new Map<String, Object> {             
        'Sort' => new Map<String, Object> {
          'field' => 'created_at',
          'order' => 'desc'
        }
      }
    };

    params = new Map<String, Object> {
      'path' => 'search?search_bar=true&q='+clientInstance.token(query)
    };
  }

  public String getParameters() {
    return JSON.serialize(params);
  }
}

By referring to the above code,

The Sort Map structure contains two keys:

  • field - corresponds to the date used to sort the images of the search results. It takes two values:
    • created_at - It is the date on which the image has been uploaded to SharinPix.
    • date_taken - It is the date on which the photo has been captured by a device.
  • order  - corresponds to the order in which the images appear in the search results. it takes these values:
    • asc - The images appear from the least recent to the most recent(based on the date defined in field)
    • desc - The images appear from the most recent to the least recent(based on the date defined in field)

Visualforce Page

The code snippet below illustrates the implementation of the Visualforce Page used to display the SharinPix Image Search.

<apex:page docType="html-5.0" cache="false"
  showHeader="false"
  sidebar="false"
  standardStylesheets="false"
  standardController="Account"
  extensions="SharinPixSearchController"
  applyHtmlTag="false"
  applyBodyTag="false">
  <apex:canvasApp developerName="Albums" width="100%" height="500px" parameters="{! parameters }"/>
</apex:page>