Last active
December 19, 2015 21:19
-
-
Save sjimenez77/6019305 to your computer and use it in GitHub Desktop.
Javascript: Función de ordenación de un array por campos para aplicarla con el método sort. Se incluyen campos de ejemplo numéricos, texto y fechas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FIXERR1002: Función de ordenación para las columnas de las vistas | |
function sortByKey(array, key, asc) { | |
return array.sort(function(a, b) { | |
if (key == "id" || | |
key == "cid" || | |
key == "eid" || | |
key == "nexp" || | |
key == "dni" || | |
key == "poblacion" || | |
key == "provincia" || | |
key == "telefono" || | |
key == "municipio" || | |
key == "poligono" || | |
key == "parcela" || | |
key == "recinto" || | |
key == "superficie" || | |
key == "supcultivo" || | |
key == "porcentaje" || | |
key == "compn" || | |
key == "compp" || | |
key == "paid" || | |
key == "aid" || | |
key == "tid" || | |
key == "lid" || | |
key == "rid" || | |
key == "totaln" || | |
key == "totalp" || | |
key == "totalk" || | |
key == "cantidad" || | |
key == "numfactura") { | |
// Valores numéricos | |
var x = parseInt(a[key]); | |
var y = parseInt(b[key]); | |
} else { | |
if (key.indexOf("fecha") != -1) { | |
// Fecha | |
var partes_fecha_x = a[key].split('/'); | |
var partes_fecha_y = b[key].split('/'); | |
var x = new Date(partes_fecha_x[2], (partes_fecha_x[1] - 1), partes_fecha_x[0]); | |
var y = new Date(partes_fecha_y[2], (partes_fecha_y[1] - 1), partes_fecha_y[0]); | |
} else { | |
// Texto | |
var x = a[key].toLowerCase(); | |
var y = b[key].toLowerCase(); | |
} | |
} | |
if (asc) { | |
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); | |
} else { | |
return ((x > y) ? -1 : ((x < y) ? 1 : 0)); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment