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

244
Vistas
How to pass sort direction to a function?

I have the following code:

    this.userActionsRights$ = combineLatest([this.sortBy$, this.userActionsRights$]).pipe(
        map(([sortBy, userRights]) => {
            const { active, direction } = sortBy;
            const sortByAsc = direction === 'asc';
            let sortFn;

            if (active === 'name') sortFn = compareString;
            if (active === 'id') sortFn = compareNumber;

            return userRights.sort(sortFn);
        }),
    );

Where compare string is:

export function compareString(a: string | undefined, b: string | undefined, isAsc: boolean) {
  if (a === undefined || a === null) return isAsc ? 1 : -1
  if (b === undefined || b === null) return isAsc ? -1 : 1

  return isAsc ? b.localeCompare(a) : a.localeCompare(b)
}

How to pass sortByAsc to compareString and then apply it below in userRights.sort(sortFn);.

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

0

You can just create a new arrow function on the fly, like so:

this.userActionsRights$ = combineLatest([this.sortBy$, this.userActionsRights$])
  .pipe(
    map(([sortBy, userRights]) => {
      const { active, direction } = sortBy;
      const sortByAsc = direction === 'asc';
      let sortFn;

      // Here instead of passing the method directly,
      // we create a new one with the correct parameters passed
      if (active === 'name') sortFn = (a, b) => compareString(a, b, sortByAsc);
      if (active === 'id') sortFn = compareNumber;

      return userRights.sort(sortFn);
    }),
  );
about 4 years ago · Juan Pablo Isaza Denunciar

0

An easy way to solve your problem would be to wrap sortFn in a function that inverts the result if the sort direction is "desc".

const userRights = /* an array */;
const sortFn = /* sort callback function that sorts asc */;
const direction = /* the sort direction */;
const modifiers = { asc: 1, desc: -1 };

// Forward the `a` and `b` arguments to `sortFn` and multiply the return
// value with the modifier.
return userRights.sort((a, b) => sortFn(a, b) * modifiers[direction]);

Note that direction must be either "asc" or "desc", otherwise NaN is returned. (A number multiplied by undefined results in NaN.)

To allow both uppercase and lowercase characters you might want to downcase the direction up front.

direction = direction.toLowerCase(); // <- you have to define direction with `let`

You probably also want to throw an error if an invalid direction is passed.

if (!(direction in modifiers)) {
  throw new Error(`Invalid sort direction "${direction}".`);
}
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