Good afternoon I have a query, I want to order a json that I get after consuming an api, to show it in a datatable, the question is that I do not want to order it in the backend, but just before passing it to the datatable, so that it is displayed directly in the datatable for the property that you specify.
This is my code with which I subscribe and get my data
My JSON
RenderDataTable() {
this.isLoading=true; // El Spinner se muestra mientras se estan consumiendo los datos de la Api.
this.PrtgService.getAllElements(this.table).subscribe(
(res) => {
this.dataSource = new MatTableDataSource();
this.dataSource.data = res; // Asigando a lo que me estoy subscribiendo para asignarlo a un DataSource
this.dataSource.paginator = this.paginator; // Paginando el DataSource
this.isLoading=false; // Si la carga de datos proveniente de la Api fue existosa, dejo de mostrar el Spinner
// console.log(res); // Imprimo para comporbar que todo Ok y estoy consumiendo lo deseado.
this.LengthEquipos=res.length; // Obtengo la longitud del arrray que me traigo de la API.
// console.log(this.LengthEquipos);
},
(error) => {
console.log('Se produjo un error mientras intentaba recuperar '+this.table + '!' + error);
});
}
And as my answer shows:
But in my case I want to get my data ordered by the Vendedor property


resto the data, assignres.sort()and pass a callback to it? TheArray.sort()method can take a callback function, which has two parameters, which are two items from the array. If it returns a positive number, the first item gets moved after the second, if it returns a negative number, the first item gets moved BEFORE the second. Hint: thelocaleComparemethod of strings returns just such a number for two strings based on their alphabetical order.