简体   繁体   中英

Sort array by Object Property on javascript

I have this code and i need to sort each element of the array by area .

function Rectangle(base, altura) {
  this.base = base;
  this.altura = altura;

  this.area = function () {
    return this.base * this.altura;
  };

  this.perimetre = function () {
    return 2 * (this.base + this.altura);
  };

  this.toString = function () {
    return (
      '(b= ' +
      this.base +
      ', h= ' +
      this.altura +
      ', a = ' +
      this.area() +
      ', p =' +
      this.perimetre() +
      ')'
    );
  };
}
var rectangles = [
  new Rectangle(1, 1),
  new Rectangle(2, 2.05),
  new Rectangle(2, 5),
  new Rectangle(1, 3),
  new Rectangle(4, 4),
  new Rectangle(2, 8)
];

I need to do it by first declaring the function in the Array class of javascript and then sort it by using the sort() method.

array.prototype.ordenaPerArea = function() {
            
};

How can I do this?

You can use just subtract the areas for comparing.

 function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return (this.base * this.altura); } this.perimetre = function() { return 2 * (this.base + this.altura); } this.toString = function() { return "(b= " + this.base + ", h= " + this.altura + ", a = " + this.area() + ", p =" + this.perimetre() + ")"; } } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; rectangles.sort((a, b) => a.area() - b.area()); rectangles.forEach(x => console.log(x.toString()));

If you really need to modify Array's prototype, you could, but it is not recommended.

 function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return (this.base * this.altura); } this.perimetre = function() { return 2 * (this.base + this.altura); } this.toString = function() { return "(b= " + this.base + ", h= " + this.altura + ", a = " + this.area() + ", p =" + this.perimetre() + ")"; } } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; Array.prototype.ordenaPerArea = function(){ return this.sort((a, b) => a.area() - b.area()); } rectangles.ordenaPerArea(); rectangles.forEach(x => console.log(x.toString()));

I think what you are looking for is:

Method modifying the original array:

 function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return this.base * this.altura; }; this.perimetre = function() { return 2 * (this.base + this.altura); }; this.toString = function() { return ( '(b= ' + this.base + ', h= ' + this.altura + ', a = ' + this.area() + ', p =' + this.perimetre() + ')' ); }; } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; console.log('rectangles (before):'); rectangles.forEach(item => console.log(item.area())); Array.prototype.ordenaPerArea = function() { return this.sort(function(rectA, rectB) { return rectA.area() - rectB.area(); }); } rectangles.ordenaPerArea(); console.log('rectangles (after):'); rectangles.forEach(item => console.log(item.area()));

Method returning an ordered copy of the original array:

 function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return this.base * this.altura; }; this.perimetre = function() { return 2 * (this.base + this.altura); }; this.toString = function() { return ( '(b= ' + this.base + ', h= ' + this.altura + ', a = ' + this.area() + ', p =' + this.perimetre() + ')' ); }; } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; console.log('rectangles (before):'); rectangles.forEach(item => console.log(item.area())); Array.prototype.ordenaPerArea = function() { return this.map(item => item).sort(function(rectA, rectB) { return rectA.area() - rectB.area(); }); } const orderedRectangles = rectangles.ordenaPerArea(); console.log('orderedRectangles:'); orderedRectangles.forEach(item => console.log(item.area())); console.log('rectangles (after):'); rectangles.forEach(item => console.log(item.area()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM