2

I have an array of object employees

enter image description here

    {
  "emp1": {
    "BusinessPartnerFormattedName": "Aleksandra Lewandowski",
    "EmpRoleCode": "BUP003",
    "EmpRoleType": "Employee",
    "EmployeeID": "E8000",
    "isAssigned" : true,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp2": {
    "BusinessPartnerFormattedName": "Aleksandra Lewandowski",
    "EmpRoleCode": "BUP003",
    "EmpRoleType": "Employee",
    "EmployeeID": "E8000",
    "isAssigned" : true,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp3": {
    "BusinessPartnerFormattedName": "Aleksandra Lewandowski",
    "EmpRoleCode": "BUP003",
    "EmpRoleType": "Employee",
    "EmployeeID": "E8000",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp4": {
    "BusinessPartnerFormattedName": " Lewandowski",
    "EmpRoleCode": "BUP803",
    "EmpRoleType": "Employee",
    "EmployeeID": "BUP803",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp5": {
    "BusinessPartnerFormattedName": "Aleksandra",
    "EmpRoleCode": "BUP043",
    "EmpRoleType": "Employee",
    "EmployeeID": "BUP043",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp6": {
    "BusinessPartnerFormattedName": "Eva Log",
    "EmpRoleCode": "BUP0d03",
    "EmpRoleType": "Employee",
    "EmployeeID": "BUP0d03",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  }
}

I would like to put the employee where EmployeeID is equal to E8000 first in list , than I want the employee where isAssigned is equal to true to follow

So far I've achieved this :

var sortedItems = _.sortBy(items, function(item) {
  return (item.isAssigned === true || item.EmployeeID === "E8000") ? 0 : 1;
});

But how to make sure that the emp with EmployeeID is equal E8000 to will always be on top ?

8
  • yould you have more than three items? what should happen if more? Commented Dec 5, 2019 at 21:37
  • 1
    btw, you are trying to sort an object, because the array has only one item ... Commented Dec 5, 2019 at 21:38
  • @NinaScholz Yes I have like 99 items, but my code is a simple example Commented Dec 5, 2019 at 21:40
  • But as Nina said, you can't sort an object. Did you mean an array of objects? (without these emp1, emp2 and so on) keys? Commented Dec 5, 2019 at 21:41
  • @kinduser Yes array of object, I've made some correction ( I'm sorry for confusion caused) Commented Dec 5, 2019 at 21:46

3 Answers 3

3

You chain the wanted sort order.

var array = [
        { name: "E8000 true", EmployeeID: "E8000", isAssigned : true },
        { name: "E8000 true", EmployeeID: "E8000", isAssigned : true },
        { name: "E8003 false", EmployeeID: "E8003", isAssigned : false },
        { name: "E8001 true", EmployeeID: "E8001", isAssigned : true },
        { name: "E8002 true", EmployeeID: "E8002", isAssigned : true },
        { name: "E8001 false", EmployeeID: "E8001", isAssigned : false }
    ];

array.sort((a, b) => 
    (b.EmployeeID === 'E8000') - (a.EmployeeID === 'E8000') ||
    b.isAssigned - a.isAssigned
);

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

With lodash

var array = [
        { name: "E8000 true", EmployeeID: "E8000", isAssigned : true },
        { name: "E8000 true", EmployeeID: "E8000", isAssigned : true },
        { name: "E8003 false", EmployeeID: "E8003", isAssigned : false },
        { name: "E8001 true", EmployeeID: "E8001", isAssigned : true },
        { name: "E8002 true", EmployeeID: "E8002", isAssigned : true },
        { name: "E8001 false", EmployeeID: "E8001", isAssigned : false }
    ];

console.log(_.sortBy(array, [
    o => o.EmployeeID !== 'E8000',
    o => !o.isAssigned
]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

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

1 Comment

Exactly what I wanted danke schon :)
3

Not sure why you need lodash on this:

items.sort((a,b)=>{
    if(a.EmployeeID !== b.EmployeeID){
        if(a.EmployeeID === 'E8000') return -1;
        if(b.EmployeeID === 'E8000') return 1;
    };

    if(a.isAssigned !== b.isAssigned){
        return a.isAssigned ? -1 : 1;
    }

    //Remaining sort logic...
})

This translates to:

"When deciding if a or b should come first, if a and b don't both have the same EmployeeID value, check if either has value E8000. If ether one does have that value, put that one first.

Otherwise, check if a and b have the same value for isAssigned, if they don't, put the one that has true as the isAssigned value first.

Finally if the prior conditions weren't met, follow whatever logic you want for setting which should go first.

Of course, you have to fill in whatever sort logic you want to come after that. For example, if you want to sort alphabetically on BusinessPartnerFormattedName, you'd fill that Remaining sort logic... block with:

if(a.BusinessPartnerFormattedName < b.BusinessPartnerFormattedName) return -1;
if(a.BusinessPartnerFormattedName > b.BusinessPartnerFormattedName) return 1;
return 0;

4 Comments

I just love lodash, personal choice but thank for your vanilla approach
It doesn't make any sense... OP has an array of objects that have a nested object inside... Your solution will not solve it
@kinduser- You're correct! Sorry, I thought the top level object was an array. napi15, this will NOT work if your top level object is not an array.
@DRich Seems like it fits OP issue:)
1

The data you provided is highly misleading, however I still would like to publish my solution, because basically I just like it.

const items = [{BusinessPartnerFormattedName:"Aleksandra Lewandowski",EmpRoleCode:"BUP003",EmpRoleType:"Employee",EmployeeID:"E7000",isAssigned:!0,ObjectID:"00163E0E46241ED7A0EA0590D0655967"},{BusinessPartnerFormattedName:"Aleksandra Lewandowski",EmpRoleCode:"BUP003",EmpRoleType:"Employee",EmployeeID:"E6000",isAssigned:!1,ObjectID:"00163E0E46241ED7A0EA0590D0655967"},{BusinessPartnerFormattedName:"Aleksandra Lewandowski",EmpRoleCode:"BUP003",EmpRoleType:"Employee",EmployeeID:"E8000",isAssigned:!1,ObjectID:"00163E0E46241ED7A0EA0590D0655967"}];
const helper = (emp) => emp.EmployeeID === 'E8000' ? 2 : +(!!emp.isAssigned);

const r = items.sort((a, b) => helper(b) - helper(a));

console.log(r);

3 Comments

Thank you for your answer, however I would like to improve my question, would you mind telling me what is misleading so I can edit it ?
@napi15 So basically you have posted an array of objects, but inside these objects is a nested object with key "emp1", "emp2" and so on, and value of these keys is the employee object. So its like a nested structure, however all of published answers are related with flat array of objects (without these "emp1", "emp2" keys), just an array of employee objects
Oh ok , yes I noticed that all provided answer does not correspond to my nested structure , but I adapt the provided logic and thank you again for your answer

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.