Advertisement
kstoyanov

01. Order Rectangles

Oct 11th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function orderRectangles(input) {
  2.   class Rectangle {
  3.     constructor(width, height) {
  4.       this.width = width;
  5.       this.height = height;
  6.     }
  7.  
  8.     area() {
  9.       return this.width * this.height;
  10.     }
  11.  
  12.     compareTo(other) {
  13.       return other.area() - this.area() || other.width - this.width;
  14.     }
  15.   }
  16.  
  17.   const rectangles = [];
  18.  
  19.   input.forEach((rectangleValues) => {
  20.     const [width, height] = rectangleValues;
  21.     const rectangle = new Rectangle(width, height);
  22.     rectangles.push(rectangle);
  23.   });
  24.  
  25.   rectangles.sort((a, b) => a.compareTo(b));
  26.  
  27.   return rectangles;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement