Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
¿Cómo pasar la dirección de clasificación a una función?

Tengo el siguiente código:

 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); }), );

Donde la cadena de comparación es:

 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) }

Cómo pasar sortByAsc a compareString y luego aplicarlo a continuación en userRights.sort(sortFn); .

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simplemente puede crear una nueva función de flecha sobre la marcha, así:

 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 Report

0

Una manera fácil de resolver su problema sería envolver sortFn en una función que invierte el resultado si la dirección de clasificación es "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]);

Tenga en cuenta que la direction debe ser "asc" o "desc" , de lo contrario, se devuelve NaN . (Un número multiplicado por resultados undefined en NaN ).

Para permitir caracteres tanto en mayúsculas como en minúsculas, es posible que desee poner en minúsculas la direction al principio.

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

Probablemente también desee arrojar un error si se pasa una dirección no válida.

 if (!(direction in modifiers)) { throw new Error(`Invalid sort direction "${direction}".`); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!