Advertisement
Nikolcho

01. Order Rectangles

Jun 15th, 2020
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(recs) {
  2.     const orderedRecs = [];
  3.     for (let rec of recs) {
  4.         [w, h] = rec;
  5.         orderedRecs.push({
  6.             width: w,
  7.             height: h,
  8.             area: function() { return this.width * this.height},
  9.             compareTo: function(other) {
  10.                 return other.area() - this.area() || other.width - this.width;
  11.             }
  12.         });
  13.     }
  14.  
  15.     orderedRecs.sort((a, b) => a.compareTo(b));
  16.  
  17.     return orderedRecs;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement