How to order an area by means of your property in javascript?

2
var apartamentos = [{
  fecha_ingreso: "10/10/11", 
  tipo_inmueble:"En venta", 
  precio: "1000", 
  titulo: "Edificio",
  metraje: 3,
  ubicacion: '14 Avenida 16-01 Zona 10. Guatemala, Guatemala',
  zona: "Zona 3", 
  habitaciones: 3, 
   parqueos: 5, 
  banos: 2, 
  tipo_techo: "Balcon", 
 categoria: 'Gimnasio'
  }]

Try this but it did not work:

function filtrarporfecha(){
var ordenarpor = $('input:radio[name=ordenarpor]:checked').val();

if(ordenarpor == "nuevo"){
    apartamentos.sort();


}
}
    
asked by Angel 08.06.2018 в 23:49
source

2 answers

3

The sort function receives another function as a parameter. You can customize the order using any property. I leave an example, I hope you serve chapin.

var apartamentos = [{
  fecha_ingreso: "10/10/11", 
  tipo_inmueble:"En venta", 
  precio: "1000", 
  titulo: "Edificio",
  metraje: 3,
  ubicacion: '14 Avenida 16-01 Zona 10. Guatemala, Guatemala'  
  },
  {
  fecha_ingreso: "11/10/11", 
  tipo_inmueble:"En venta", 
  precio: "1000", 
  titulo: "Edificio",
  metraje: 3,
  ubicacion: '14 Avenida 16-01 Zona 10. Guatemala, Guatemala'  
}]

function filtrarporfecha(){
      
    apartamentos.sort(function(a,b){ 
    
        return new Date(b.fecha_ingreso) - new Date(a.fecha_ingreso);
    });    
      
}
    
filtrarporfecha();
console.log(apartamentos);
    
answered by 09.06.2018 / 00:03
source
0

Syntax arr.sort ([compareFunction]) Description: If compareFunction is not provided, the elements are ordered by converting them to strings and comparing the position of the value Unicode of said strings. Example:

"Cherry" it comes before "banana". In a numerical order, 9 is before 80, but since the numbers are converted to strings and ordered according to the Unicode value, the result will be "80" before "9".

var frutas = ['guindas', 'manzanas', 'bananas'];    
frutas.sort(); // ['bananas', 'guindas', 'manzanas']

var puntos = [1, 10, 2, 21];    
puntos.sort(); // [1, 10, 2, 21]

// Keep in mind that 10 comes before 2 // because '10' comes before '2' according to the position of the Unicode value.

var cosas = ['word', 'Word', '1 Word', '2 Words'];    
cosas.sort();  // ['1 Word', '2 Words', 'Word', 'word']

// In Unicode, the numbers come before the uppercase letters // and these come before the letters lowercase.

    
answered by 05.07.2018 в 22:28