Blog

Tutorial: Amazon.com Search API

Posted on

10. Result Sorting

Result Sorting Code

// Result Sorting
   var sortBy = cel('div');
   sortBy.setAttribute("id", "sortBy");
   var sortByem = cel('em');
   sortByem.appendChild(ctn("Sort By"));
   sortBy.appendChild(sortByem);
   var sortByul = cel('ul');
   var sortByli1 = cel('li');
   var sortByli2 = cel('li');
   var sortByli3 = cel('li');
   var popularity = cel('a');
   if (in_array(tmpData.ItemSet.sort, bssort))
   {
      popularity.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #999999; font-weight:bold;";
   }
   else
   {
      popularity.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', 'salesrank', '" + tmpData.ItemSet.category + "');");
      popularity.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
   }
   popularity.appendChild(ctn("BestSelling"));
   var featured = cel('a');
   if (in_array(tmpData.ItemSet.sort, featuredsort))
   {
      featured.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #999999; font-weight:bold;";
   }
   else
   {
      featured.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', 'psrank', '" + tmpData.ItemSet.category + "');");
      featured.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
   }
   featured.appendChild(ctn("Featured"));
   var pricesort = cel('a');
   if (in_array(tmpData.ItemSet.sort, priceup))
   {
      pricesort.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', 'price', '" + tmpData.ItemSet.category + "');");
      pricesort.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
      pricesort.appendChild(ctn("Price" + "u25bc"));
   }
   else
   {
      pricesort.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', '-price', '" + tmpData.ItemSet.category + "');");
      pricesort.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
      pricesort.appendChild(ctn("Price" + "u25b2"));
   }
   sortByli1.appendChild(popularity);
   sortByli2.appendChild(featured);
   sortByli3.appendChild(pricesort);
   sortByul.appendChild(sortByli1);
   sortByul.appendChild(sortByli2);
   sortByul.appendChild(sortByli3);
   sortBy.appendChild(sortByul);

Category Specific Sort Value Arrays

// Sort Values
// Featured

var featuredsort = Array();
featuredsort["Baby"] = "psrank";
featuredsort["Beauty"] = "pmrank";
featuredsort["Classical"] = "psrank";
featuredsort["Electronics"] = "pmrank";
featuredsort["HealthPersonalCare"] = "pmrank";
featuredsort["Jewelry"] = "pmrank";
featuredsort["Kitchen"] = "pmrank";
featuredsort["Miscellaneous"] = "pmrank";
featuredsort["Music"] = "psrank";
featuredsort["MusicalInstruments"] = "pmrank";
featuredsort["OfficeProducts"] = "pmrank";
featuredsort["OutdoorLiving"] = "psrank";
featuredsort["PCHardware"] = "psrank";
featuredsort["PetSupplies"] = "+pmrank";
featuredsort["Photo"] = "pmrank";
featuredsort["Software"] = "pmrank";
featuredsort["Tools"] = "pmrank";
featuredsort["Toys"] = "pmrank";
featuredsort["VideoGames"] = "pmrank";
featuredsort["WirelessAccessories"] = "psrank";

// Price

var priceup = Array();
priceup['Apparel'] = 'inverseprice';
priceup['Automotive'] = '-price';
priceup['Baby'] = '-price';
priceup['Beauty'] = '-price';
priceup['Books'] = 'inverse-pricerank';
priceup['Classical'] = '-price';
priceup['DVD'] = '-price';
priceup['Electronics'] = '-price';
priceup['GourmetFood'] = 'inverseprice';
priceup['Grocery'] = 'inverseprice';
priceup['HealthPersonalCare'] = 'inverseprice';
priceup['HomeGarden'] = '-price';
priceup['Jewelry'] = 'inverseprice';
priceup['Kitchen'] = '-price';
priceup['Magazines'] = '-price';
priceup['Merchants'] = 'inverseprice';
priceup['Miscellaneous'] = '-price';
priceup['Music'] = '-price';
priceup['MusicalInstruments'] = '-price';
priceup['OfficeProducts'] = '-price';
priceup['OutdoorLiving'] = '-price';
priceup['PCHardware'] = '-price';
priceup['PetSupplies'] = '-price';
priceup['Photo'] = '-price';
priceup['Software'] = '-price';
priceup['SportingGoods'] = 'inverseprice';
priceup['Tools'] = '-price';
priceup['Toys'] = '-price';
priceup['VHS'] = '-price';
priceup['Video'] = '-price';
priceup['VideoGames'] = '-price';
priceup['Wireless'] = 'inverse-pricerank';

var pricedown = Array();
pricedown['Apparel'] = 'pricerank';
pricedown['Automotive'] = 'price';
pricedown['Baby'] = 'price';
pricedown['Beauty'] = 'price';
pricedown['Books'] = 'pricerank';
pricedown['Classical'] = 'price';
pricedown['DVD'] = 'price';
pricedown['Electronics'] = 'price';
pricedown['GourmetFood'] = 'pricerank';
pricedown['Grocery'] = 'pricerank';
pricedown['HealthPersonalCare'] = 'pricerank';
pricedown['HomeGarden'] = 'price';
pricedown['Jewelry'] = 'pricerank';
pricedown['Kitchen'] = 'price';
pricedown['Magazines'] = 'price';
pricedown['Merchants'] = 'pricerank';
pricedown['Miscellaneous'] = 'price';
pricedown['Music'] = 'price';
pricedown['MusicalInstruments'] = 'price';
pricedown['OfficeProducts'] = 'price';
pricedown['OutdoorLiving'] = 'price';
pricedown['PCHardware'] = 'price';
pricedown['PetSupplies'] = 'price';
pricedown['Photo'] = 'price';
pricedown['Software'] = 'price';
pricedown['SportingGoods'] = 'pricerank';
pricedown['Tools'] = 'price';
pricedown['Toys'] = 'price';
pricedown['VHS'] = 'price';
pricedown['Video'] = 'price';
pricedown['VideoGames'] = 'price';
pricedown['Wireless'] = 'pricerank';

// BestSelling

var bssort = Array();
bssort['Apparel'] = 'salesrank';
bssort['Automotive'] = 'salesrank';
bssort['Baby'] = 'salesrank';
bssort['Beauty'] = 'salesrank';
bssort['Books'] = 'salesrank';
bssort['Classical'] = 'salesrank';
bssort['DigitalMusic'] = 'songtitlerank';
bssort['DVD'] = 'salesrank';
bssort['Electronics'] = 'salesrank';
bssort['GourmetFood'] = 'salesrank';
bssort['Grocery'] = 'salesrank';
bssort['HealthPersonalCare'] = 'salesrank';
bssort['HomeGarden'] = 'salesrank';
bssort['Jewelry'] = 'salesrank';
bssort['Kitchen'] = 'salesrank';
bssort['Magazines'] = 'subslot-salesrank';
bssort['Merchants'] = 'salesrank';
bssort['Miscellaneous'] = 'salesrank';
bssort['Music'] = 'salesrank';
bssort['MusicalInstruments'] = 'salesrank';
bssort['OfficeProducts'] = 'salesrank';
bssort['OutdoorLiving'] = 'salesrank';
bssort['PCHardware'] = 'salesrank';
bssort['PetSupplies'] = 'salesrank';
bssort['Photo'] = 'salesrank';
bssort['Software'] = 'salesrank';
bssort['SportingGoods'] = 'salesrank';
bssort['Tools'] = 'salesrank';
bssort['Toys'] = 'salesrank';
bssort['VHS'] = 'salesrank';
bssort['Video'] = 'salesrank';
bssort['VideoGames'] = 'salesrank';
bssort['Wireless'] = 'salesrank';
bssort['WirelessAccessories'] = 'salesrank';

Perhaps the most difficult part of this tutorial is the result sorting functionality. Amazon does not use uniform parameters for sorting items in different categories. Each category contains its own, sometimes unique, group of sort values. After all, it wouldn’t make any sense to sort a Gourmet Food by Artist (unless you live in France). Therefore, the sorting options are configured by the current product category and sort index using associative arrays based on sort type and search index.

There are three options to sort results – Featured, BestSelling and Price. These are the most common sort indexes that Amazon offers. The “Sort By” <div> element contains three links for each sorting option. Like the paginate function, the links are javascript function calls to the search function, each with a different sort index variable; i.e., priceup for ascending price.

The associative arrays are grouped by sort index, with name : value pairs corresponding to Category : Parameter. For instance, the value (or parameter) of priceup[‘VideoGames’] will be different from priceup[‘Wireless’]. The sort parameter that will be sent to Amazon is determined in the search function by matching the product search category to a parameter in the array containing the desired sort index per the below code snippet.

Sort Parameter Selection

// Determine Sort Variable
   if (s &amp;&amp; c)
   {
      if ((s == 'salesrank') &amp;&amp; (bssort))
      {
         s = bssort;
      }
      else if ((s == 'psrank') &amp;&amp; (featuredsort))
      {
         s = featuredsort;
      }
      else if ((s == 'price') &amp;&amp; (pricedown))
      {
         s = pricedown;
      }
      else if ((s == '-price') &amp;&amp; (priceup))
      {
         s = priceup;
      }
      else
      {
         s = '';
      }
   }