Yoda
Yoda

Reputation: 18068

Lodash sort according to the provided comparator

Is it possible to sort by a provided comparator by any lodash function?

For instance I have class Person:

class Person{name: string; age: numbert}

and then I want to sort people in the way that people of age 0 are in the end and rest is sorted in the ascending order? People are of type {}.

_.sortBy(people, (p1,p2) => { 
          if(p1.age == 0) return -1; 
      return p1.age - p2.age 
}

Upvotes: 1

Views: 694

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97227

Slightly hacky, but this will work:

_.sortBy(people, [p => p.age || Infinity])

Upvotes: 1

Related Questions