1

here is my case I am trying to sort an array with two name and crop equal true I am using lodash sortBy method

https://lodash.com/docs/4.17.11#sortBy

it working just fine with sorting string but not working with boolean filed

here is my code

 const unsortedArray = [
      { name: "mina", lastName: "a", crop: false },
      { name: "aaa", lastName: "fa", crop: true },
      { name: "mina", lastName: "a", crop: true }
    ];
    console.log("un sorted array ", unsortedArray);
    console.log(
      " sorted array ",
      _.sortBy(unsortedArray , ["name" ,"crop"])
    );
2
  • It does work. The crop field is being treated as a string and it's being sorted in alphabetical order. How do you want it? Commented Jun 12, 2019 at 18:40
  • I need to sort against its boolean value mean true goes up, false goes down Commented Jun 12, 2019 at 18:45

1 Answer 1

1

Use

_.orderBy(unsortedArray , ["name" ,"crop"], ["asc", "desc"]);
Sign up to request clarification or add additional context in comments.

1 Comment

Glad that it helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.