Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
Sorting a array. Help me to understand

im new in javaScript and i need to understand why my array is not sorting in alphabetic order.

Probably im missing something. Somebody can help me?

This is my TS

  getEmpresas() {

this.empresaService.getAll().subscribe({
  next: empresas => this.empresas = empresas,
  error: err => console.log(err),
});
let empresas = this.empresas.filter(obj => obj?.nome.toLowerCase());
empresas.sort(function (a, b) {
  if (a.nome < b.nome) { return -1; }
  if (a.nome > b.nome) { return 1; }
  return 0;

})
return empresas;

}

sry i paste a wrong one.

and here the class Empresa

  export class Empresa {
  id: number;
  nome: string;
  nomeCurto: string;
  inscricaoEstadual: string;
  inscricaoMunicipal: string;
  cnpj: string;
  telefone: string;
  email: string;
  caminhoLogoTipo: string;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

A subscription is asynchronous. This means the line this.empresas = empresas will not be executed before your sorting function. Just put your sorting call inside the subscription, and don't bother returning a value. this.emprasas will be populated and sorted after receiving a response from your service.

Also, I believe you are trying to convert all names to lower case with filter. This is not what filter does, I've exchanged this line with a call to map that will convert all names to lower case.

  getEmpresas() {
    this.empresaService.getAll().subscribe({
      next: (empresas) => {
        empresas = this.empresas.map((obj) => {
          return { ...obj, nome: obj?.nome.toLowerCase() };
        });
        empresas.sort(function (a, b) {
          if (a.nome < b.nome) return -1;
          if (a.nome > b.nome) return 1;
          return 0;
        });
        this.empresas = empresas;
      },
      error: (err) => console.log(err),
    });
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this:

getEmpresas() {
    this.empresaService.getAll().subscribe({
      next: (empresas) => {
        empresas = this.empresas.map((obj) => {
          return { ...obj, nome: obj?.nome.toLowerCase() };
        });
        empresas.sort((a,b) => (a.nome > b.nome) ? 1 : ((b.nome > a.nome) ? -1 : 0))
        this.empresas = empresas;
      },
      error: (err) => console.log(err),
    });
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda