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

163
Vistas
Sorting array regardless of upper or lowercase

I am creating this sorting pipe for my angular project. But it comes out as this.

"BUNNY",
"Castle 111",
"TOTAL 2323 Test",
"Tower 2323 Test",
"Vivy",
"bayside"
I want to be [bayside,BUNNY,Castle 111,TOTAL 2323 Test,Tower 2323 Test,vivy] Here is my test code snippet https://codesandbox.io/s/orderbypipe-bc7xx?file=/src/app/app.component.html:820-832

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use localeCompare with {sensitivity: 'accent'} in the sort callback to ignore case.

const arr=["BUNNY",
"Castle 111",
"TOTAL 2323 Test",
"Tower 2323 Test",
"Vivy",
"bayside"];
arr.sort((a,b)=>a.localeCompare(b, undefined, {sensitivity: 'accent'}));
console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can compare the two strings by converting both to lowercase (with String.toLowerCase), then using String.localeCompare:

const arr = ["BUNNY",
  "Castle 111",
  "TOTAL 2323 Test",
  "Tower 2323 Test",
  "Vivy",
  "bayside"
]

const sorted = arr.sort((a,b) => a.toLowerCase().localeCompare(b.toLowerCase()))

console.log(sorted)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can leverage the sortBy method of lodash library for sorting the one-dimensional collection irrespective of the case.

  • Install lodash and lodash-es
npm install --save lodash

npm install --save-dev @types/lodash
  • Modify the pipe as shown in the following snippet,
import { Pipe, PipeTransform } from "@angular/core";
import { sortBy } from "lodash-es";

@Pipe({ name: "sortBy" })
export class SortByPipe implements PipeTransform {
  transform<T>(
    value: T[],
    caseInsensitive = false,
    order = "",
    column: string = ""
  ): T[] {
    if (!column || column === "") {
      const sorted = this.sortOnCaseSensitivity(value, caseInsensitive);
      if (order === "asc") {
        return sorted;
      } else {
        return sorted.reverse();
      }
    }
    if (value.length <= 1) {
      return value;
    }
  }
  sortOnCaseSensitivity<T>(value: T[], caseInsensitive: boolean): T[] {
    return sortBy(value, (v: T) => {
      if (typeof v === "string" && caseInsensitive) {
        return v.toLowerCase();
      }
      return v;
    });
  }
  
}
  • Call the created sortBy custom pipe in your template.html file as shown below,
<ul>
  <li *ngFor="let word of words | sortBy: true:'asc'">{{ word }}</li>
</ul>

For your reference adding link to a working demo here

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