Advertisement
Venciity

execute Find Rents By Price Command [ Stable sort ]

Nov 8th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function executeFindRentsByPriceCommand(minPrice, maxPrice) {
  2.     if (isNaN(minPrice) || isNaN(maxPrice)) {
  3.         throw new Error("Invalid price range.");
  4.     }
  5.  
  6.     var selectedOffers = _offers.filter(function (offer) {
  7.         return offer.getPrice() >= minPrice && offer.getPrice() <= maxPrice && offer instanceof RentOffer;
  8.     });
  9.     selectedOffers.sort(function (a, b) {
  10.         // stable sort
  11.         var result = a.getPrice() - b.getPrice();
  12.         if (result == 0) {
  13.             result = a.getEstate().getName().localeCompare(b.getEstate().getName());
  14.         }
  15.  
  16.         return result;
  17.     });
  18.  
  19.     return formatQueryResults(selectedOffers);
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement