sort from largest to smallest array of objects extracted from firebase with angularfire2

0

I need to sort this array of objects

  ben: FirebaseListObservable<any>;
  e93menor = 9999;
  e93mayor = 0;

  this.ben = this.database.list('/Bencina');
  this.ben.forEach(element =>{
       this.b = [];
       element.forEach(ele =>{
           if(ele.idCiudad == this.idCiudadGlobal){
               //console.log(ele)
             if(ele.idTipoBencina == this.idTipoBencina){
                if(parseInt(ele.precioBencina) >= this.e93mayor){                        
                        this.e93mayor = ele.precioBencina;
  //SEGÚN MI LÓGICA AQUI DEBERIA GUARDARME LOS ELEMENTOS DE MAYOR A MENOR  
                        this.b.push(ele);
                        console.log(ele);
                }    
            }else{
            }

         }
     })

})

    
asked by Dagg 24.08.2017 в 21:51
source

2 answers

1

You have it well mounted, you just have to return it in the mapping.

this.ben = this.database.list('/Bencina')
  .map(arr => arr.filter(el => {
    el.idCiudad == this.idCiudadGlobal &&
    el.idTipoBencina == this.idTipoBencina &&
    parseInt(el.precioBencina) >= this.e93mayor
  }).map(el => {
    // ¿Estás seguro sobreescribir el valor? 
    this.e93mayor = el.precioBencina;
    return el;
  })
});
    
answered by 24.08.2017 в 22:13
0

Here is a small function I did to order an array.

/**
 * Famosa función para acomodar crono o alfabéticamente un array
 * @param data El array con los datos
 * @param key El campo para ordenar
 * @param orden El sentido del orden
 * @example sortJSON(miarray, 'fecha', 'asc')
 * @copyright 2016, Fernando Magrosoto V.
 */
function sortJSON(data, key, orden) {
  return data.sort(function (a, b) {
    var x = a[key],
      y = b[key];

    if (orden === 'asc') {
      return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }

    if (orden === 'desc') {
      return ((x > y) ? -1 : ((x < y) ? 1 : 0));
    }
  });
}

From Firebase, you store the result of the snap.val () in an array and order it. I hope it serves you.

    
answered by 02.09.2017 в 00:21