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

245
Views
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 answers
Answer question

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 Report

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 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!