1

I have an array of N objects and each object has the following fields:

{
  id: 'an id',
  description: 'a description',
  isUser: true/false
}

I need to order that array based on two things:

Firstly, if the description includes either 'dog', 'cat' or 'animal', it should have sooner position in the array. Secondly, if the value isUser is false, it should also have a sooner position in the array.

I have notice that the sort function doesn't help when you want to prioritize by value of different attributes, so I am wondering what is the best way to do this in Javascript (or Lodash, which I am also using: https://lodash.com/)

2
  • 1
    please add some data for sorting and the wanted order. Commented Mar 14, 2018 at 13:37
  • What do you mean by sooner position? do you want object with properties isUser: false and description: 'dog'/'cat' or 'animal' to appear before the rest other elements ? Commented Mar 14, 2018 at 13:49

4 Answers 4

1

You could check if the wanted top string are in the description and move these item to top, as well as false values of isUser.

var array = [{ id: 1, description: 'cat', isUser: false }, { id: 5, description: 'dog', isUser: true }, { id: 6, description: 'flower', isUser: true }, { id: 7, description: 'cat', isUser: true }, { id: 2, description: 'animal', isUser: false }, { id: 3, description: 'tree', isUser: false }, { id: 4, description: 'dog', isUser: false }];
		
array.sort(function (a, b) {
    var top = ['dog', 'cat', 'animal'];
    return top.includes(b.description) - top.includes(a.description) || a.isUser - b.isUser;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Nina one question, why in the includes case you put B description first, and when doing the isUser you put the A first? So, why not do: return top.includes(b.description) - top.includes(a.description) || b.isUser - a.isUser;?
1

var test = [{
  id: 'an id',
  description: 'a description dog',
  isUser: false
},{
  id: 'an id 2',
  description: 'a description animal',
  isUser: true
},{
  id: 'an id 3',
  description: 'a description',
  isUser: true
},{
  id: 'an id 4',
  description: 'a description',
  isUser: false
}]

function sort(arr){
  return arr.sort(a=> !(a.description.includes('dog')||a.description.includes('cat')||a.description.includes('animal') || !a.isUser))
}

console.log(sort(test))

Comments

0

Assuming you question is to rearrange the existing array based on the properties and conditions given.

// grouping on basis of "description" 

const animalGrp = _.groupBy(array, (t) => _.includes(t.description, 'dog') || _.includes(t.description, 'cat') || _.includes(t.description, 'animal') );

// grouping on basis of "isUser" 
const userGrp = _.groupBy(animalGrp.false, 'isUser');

// final output
const result = [...animalGrp.true, ...userGrp.true, ...userGrp.false]

Comments

0

You can pass a compareFunction to sort method of array, so the sort function should help. Example:

yourArray.sort( function(a, b) {
    if ( !a.isUser || a.description == 'animal')  return -1;
    return 1;
});

Comments

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.