Sort objects in array by multiple property (possibility does not exist)
I am trying to sort this array:
[
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
]
As you can see, there are object who doesn't have parentId
. I need to keep this one on the top and the rest will be sorted by id
.
Here's my code:
input.sort(function (a, b) {
if (typeof a.parentId === 'undefined'))
return -1;
else
return a.id - b.id;
});
It doesn't work though. Is it possible to do this?
Expected result:
[
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
},
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
}
]
Thanks in advance
javascript
add a comment |
I am trying to sort this array:
[
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
]
As you can see, there are object who doesn't have parentId
. I need to keep this one on the top and the rest will be sorted by id
.
Here's my code:
input.sort(function (a, b) {
if (typeof a.parentId === 'undefined'))
return -1;
else
return a.id - b.id;
});
It doesn't work though. Is it possible to do this?
Expected result:
[
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
},
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
}
]
Thanks in advance
javascript
if sorting in descending order then in expected resultparentId
877234003 will come first thenparentId
877234003. pls edit your question.
– Shiv Kumar Baghel
Jan 2 at 5:32
add a comment |
I am trying to sort this array:
[
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
]
As you can see, there are object who doesn't have parentId
. I need to keep this one on the top and the rest will be sorted by id
.
Here's my code:
input.sort(function (a, b) {
if (typeof a.parentId === 'undefined'))
return -1;
else
return a.id - b.id;
});
It doesn't work though. Is it possible to do this?
Expected result:
[
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
},
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
}
]
Thanks in advance
javascript
I am trying to sort this array:
[
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
]
As you can see, there are object who doesn't have parentId
. I need to keep this one on the top and the rest will be sorted by id
.
Here's my code:
input.sort(function (a, b) {
if (typeof a.parentId === 'undefined'))
return -1;
else
return a.id - b.id;
});
It doesn't work though. Is it possible to do this?
Expected result:
[
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
},
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
}
]
Thanks in advance
javascript
javascript
asked Jan 2 at 5:19
wowhcumoswowhcumos
32
32
if sorting in descending order then in expected resultparentId
877234003 will come first thenparentId
877234003. pls edit your question.
– Shiv Kumar Baghel
Jan 2 at 5:32
add a comment |
if sorting in descending order then in expected resultparentId
877234003 will come first thenparentId
877234003. pls edit your question.
– Shiv Kumar Baghel
Jan 2 at 5:32
if sorting in descending order then in expected result
parentId
877234003 will come first then parentId
877234003. pls edit your question.– Shiv Kumar Baghel
Jan 2 at 5:32
if sorting in descending order then in expected result
parentId
877234003 will come first then parentId
877234003. pls edit your question.– Shiv Kumar Baghel
Jan 2 at 5:32
add a comment |
2 Answers
2
active
oldest
votes
Need to test A and B and invert. if A is missing the prop use -1 if B use +1
let arr = [{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
const sortPredicate = (a,b) => {
if (a.hasOwnProperty('parentId') === false) {
return -1;
} else if(b.hasOwnProperty('parentId') === false) {
return 1;
} else {
return a.id - b.id;
}
};
console.log(arr.sort(sortPredicate));
beautiful, thanks master!
– wowhcumos
Jan 2 at 5:41
An possible optimization would be to return the difference between eachhasOwnProperty
check.
– CertainPerformance
Jan 2 at 9:29
add a comment |
i would suggest to use lodash js library
var arr = [
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
var sortedArr = _.orderBy(arr, 'parentId', 'desc');
console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001558%2fsort-objects-in-array-by-multiple-property-possibility-does-not-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Need to test A and B and invert. if A is missing the prop use -1 if B use +1
let arr = [{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
const sortPredicate = (a,b) => {
if (a.hasOwnProperty('parentId') === false) {
return -1;
} else if(b.hasOwnProperty('parentId') === false) {
return 1;
} else {
return a.id - b.id;
}
};
console.log(arr.sort(sortPredicate));
beautiful, thanks master!
– wowhcumos
Jan 2 at 5:41
An possible optimization would be to return the difference between eachhasOwnProperty
check.
– CertainPerformance
Jan 2 at 9:29
add a comment |
Need to test A and B and invert. if A is missing the prop use -1 if B use +1
let arr = [{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
const sortPredicate = (a,b) => {
if (a.hasOwnProperty('parentId') === false) {
return -1;
} else if(b.hasOwnProperty('parentId') === false) {
return 1;
} else {
return a.id - b.id;
}
};
console.log(arr.sort(sortPredicate));
beautiful, thanks master!
– wowhcumos
Jan 2 at 5:41
An possible optimization would be to return the difference between eachhasOwnProperty
check.
– CertainPerformance
Jan 2 at 9:29
add a comment |
Need to test A and B and invert. if A is missing the prop use -1 if B use +1
let arr = [{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
const sortPredicate = (a,b) => {
if (a.hasOwnProperty('parentId') === false) {
return -1;
} else if(b.hasOwnProperty('parentId') === false) {
return 1;
} else {
return a.id - b.id;
}
};
console.log(arr.sort(sortPredicate));
Need to test A and B and invert. if A is missing the prop use -1 if B use +1
let arr = [{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
const sortPredicate = (a,b) => {
if (a.hasOwnProperty('parentId') === false) {
return -1;
} else if(b.hasOwnProperty('parentId') === false) {
return 1;
} else {
return a.id - b.id;
}
};
console.log(arr.sort(sortPredicate));
let arr = [{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
const sortPredicate = (a,b) => {
if (a.hasOwnProperty('parentId') === false) {
return -1;
} else if(b.hasOwnProperty('parentId') === false) {
return 1;
} else {
return a.id - b.id;
}
};
console.log(arr.sort(sortPredicate));
let arr = [{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
const sortPredicate = (a,b) => {
if (a.hasOwnProperty('parentId') === false) {
return -1;
} else if(b.hasOwnProperty('parentId') === false) {
return 1;
} else {
return a.id - b.id;
}
};
console.log(arr.sort(sortPredicate));
answered Jan 2 at 5:32


BibbertyBibberty
1,9431315
1,9431315
beautiful, thanks master!
– wowhcumos
Jan 2 at 5:41
An possible optimization would be to return the difference between eachhasOwnProperty
check.
– CertainPerformance
Jan 2 at 9:29
add a comment |
beautiful, thanks master!
– wowhcumos
Jan 2 at 5:41
An possible optimization would be to return the difference between eachhasOwnProperty
check.
– CertainPerformance
Jan 2 at 9:29
beautiful, thanks master!
– wowhcumos
Jan 2 at 5:41
beautiful, thanks master!
– wowhcumos
Jan 2 at 5:41
An possible optimization would be to return the difference between each
hasOwnProperty
check.– CertainPerformance
Jan 2 at 9:29
An possible optimization would be to return the difference between each
hasOwnProperty
check.– CertainPerformance
Jan 2 at 9:29
add a comment |
i would suggest to use lodash js library
var arr = [
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
var sortedArr = _.orderBy(arr, 'parentId', 'desc');
console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
add a comment |
i would suggest to use lodash js library
var arr = [
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
var sortedArr = _.orderBy(arr, 'parentId', 'desc');
console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
add a comment |
i would suggest to use lodash js library
var arr = [
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
var sortedArr = _.orderBy(arr, 'parentId', 'desc');
console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
i would suggest to use lodash js library
var arr = [
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
var sortedArr = _.orderBy(arr, 'parentId', 'desc');
console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
var arr = [
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
var sortedArr = _.orderBy(arr, 'parentId', 'desc');
console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
var arr = [
{
"id": 877234004,
"name": "app.js",
"type": "FILE",
"parentId": 877234003
},
{
"id": 877234010,
"name": "project",
"type": "DIRECTORY"
},
{
"id": 877234002,
"name": "src",
"type": "DIRECTORY",
"parentId": 877234010
},
{
"id": 877234003,
"name": "app",
"type": "DIRECTORY",
"parentId": 877234002
}
];
var sortedArr = _.orderBy(arr, 'parentId', 'desc');
console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
edited Jan 2 at 5:33
answered Jan 2 at 5:28


Shiv Kumar BaghelShiv Kumar Baghel
1,4263921
1,4263921
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001558%2fsort-objects-in-array-by-multiple-property-possibility-does-not-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
if sorting in descending order then in expected result
parentId
877234003 will come first thenparentId
877234003. pls edit your question.– Shiv Kumar Baghel
Jan 2 at 5:32