sorting an array in the same order as another one
up vote
-1
down vote
favorite
My problem is that I have two arrays to be sorted in the same order.
They are as follows:
objects = [obj1, obj2, obj3, obj4];
zorder = [1, 3, 4, 2]
I need to order the zorder
, so that it is 1, 2, 3, 4,,that's quite easy, but I need the objects to be sorted the same way, like so:
objects = [obj1, obj4, obj2, obj3]
zorder = [1, 2, 3, 4]
I know this question has been asked before but none of the answers are working for me. All the answers I found would turn it into this:
objects = [{name:obj1, order:1},
{name:obj2, order:3},
{name:obj3, order:4},
{name:obj4, order:2}]
and then sort it from there. I cannot do that because the objects
array needs to stay exactly the same except for the order, the same is true for zorder
. I can create a new array but then I have the same problem from the start.
I'm really stuck on this and I would be very grateful for your help, thank you in advance.
Edit:
the objects array contains 4 objects.
javascript arrays
|
show 1 more comment
up vote
-1
down vote
favorite
My problem is that I have two arrays to be sorted in the same order.
They are as follows:
objects = [obj1, obj2, obj3, obj4];
zorder = [1, 3, 4, 2]
I need to order the zorder
, so that it is 1, 2, 3, 4,,that's quite easy, but I need the objects to be sorted the same way, like so:
objects = [obj1, obj4, obj2, obj3]
zorder = [1, 2, 3, 4]
I know this question has been asked before but none of the answers are working for me. All the answers I found would turn it into this:
objects = [{name:obj1, order:1},
{name:obj2, order:3},
{name:obj3, order:4},
{name:obj4, order:2}]
and then sort it from there. I cannot do that because the objects
array needs to stay exactly the same except for the order, the same is true for zorder
. I can create a new array but then I have the same problem from the start.
I'm really stuck on this and I would be very grateful for your help, thank you in advance.
Edit:
the objects array contains 4 objects.
javascript arrays
because the objects array needs to stay exactly the same except for the order
Do you mean that reassignment (or extracting into a new variable) is not an option?
– CertainPerformance
Nov 10 at 22:15
What are obj1, obj2, obj3, and so on? Strings?
– Ele
Nov 10 at 22:16
the object array is full of objects
– user9812968
Nov 10 at 22:32
How do you identify objects using the values within the arrayzorder
?
– Ele
Nov 10 at 22:34
also yes reassignment isn't an option, the objects can only exist inside the original array or the rest of my code begins to break.
– user9812968
Nov 10 at 22:42
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
My problem is that I have two arrays to be sorted in the same order.
They are as follows:
objects = [obj1, obj2, obj3, obj4];
zorder = [1, 3, 4, 2]
I need to order the zorder
, so that it is 1, 2, 3, 4,,that's quite easy, but I need the objects to be sorted the same way, like so:
objects = [obj1, obj4, obj2, obj3]
zorder = [1, 2, 3, 4]
I know this question has been asked before but none of the answers are working for me. All the answers I found would turn it into this:
objects = [{name:obj1, order:1},
{name:obj2, order:3},
{name:obj3, order:4},
{name:obj4, order:2}]
and then sort it from there. I cannot do that because the objects
array needs to stay exactly the same except for the order, the same is true for zorder
. I can create a new array but then I have the same problem from the start.
I'm really stuck on this and I would be very grateful for your help, thank you in advance.
Edit:
the objects array contains 4 objects.
javascript arrays
My problem is that I have two arrays to be sorted in the same order.
They are as follows:
objects = [obj1, obj2, obj3, obj4];
zorder = [1, 3, 4, 2]
I need to order the zorder
, so that it is 1, 2, 3, 4,,that's quite easy, but I need the objects to be sorted the same way, like so:
objects = [obj1, obj4, obj2, obj3]
zorder = [1, 2, 3, 4]
I know this question has been asked before but none of the answers are working for me. All the answers I found would turn it into this:
objects = [{name:obj1, order:1},
{name:obj2, order:3},
{name:obj3, order:4},
{name:obj4, order:2}]
and then sort it from there. I cannot do that because the objects
array needs to stay exactly the same except for the order, the same is true for zorder
. I can create a new array but then I have the same problem from the start.
I'm really stuck on this and I would be very grateful for your help, thank you in advance.
Edit:
the objects array contains 4 objects.
javascript arrays
javascript arrays
edited Nov 10 at 22:46
asked Nov 10 at 22:13
user9812968
34
34
because the objects array needs to stay exactly the same except for the order
Do you mean that reassignment (or extracting into a new variable) is not an option?
– CertainPerformance
Nov 10 at 22:15
What are obj1, obj2, obj3, and so on? Strings?
– Ele
Nov 10 at 22:16
the object array is full of objects
– user9812968
Nov 10 at 22:32
How do you identify objects using the values within the arrayzorder
?
– Ele
Nov 10 at 22:34
also yes reassignment isn't an option, the objects can only exist inside the original array or the rest of my code begins to break.
– user9812968
Nov 10 at 22:42
|
show 1 more comment
because the objects array needs to stay exactly the same except for the order
Do you mean that reassignment (or extracting into a new variable) is not an option?
– CertainPerformance
Nov 10 at 22:15
What are obj1, obj2, obj3, and so on? Strings?
– Ele
Nov 10 at 22:16
the object array is full of objects
– user9812968
Nov 10 at 22:32
How do you identify objects using the values within the arrayzorder
?
– Ele
Nov 10 at 22:34
also yes reassignment isn't an option, the objects can only exist inside the original array or the rest of my code begins to break.
– user9812968
Nov 10 at 22:42
because the objects array needs to stay exactly the same except for the order
Do you mean that reassignment (or extracting into a new variable) is not an option?– CertainPerformance
Nov 10 at 22:15
because the objects array needs to stay exactly the same except for the order
Do you mean that reassignment (or extracting into a new variable) is not an option?– CertainPerformance
Nov 10 at 22:15
What are obj1, obj2, obj3, and so on? Strings?
– Ele
Nov 10 at 22:16
What are obj1, obj2, obj3, and so on? Strings?
– Ele
Nov 10 at 22:16
the object array is full of objects
– user9812968
Nov 10 at 22:32
the object array is full of objects
– user9812968
Nov 10 at 22:32
How do you identify objects using the values within the array
zorder
?– Ele
Nov 10 at 22:34
How do you identify objects using the values within the array
zorder
?– Ele
Nov 10 at 22:34
also yes reassignment isn't an option, the objects can only exist inside the original array or the rest of my code begins to break.
– user9812968
Nov 10 at 22:42
also yes reassignment isn't an option, the objects can only exist inside the original array or the rest of my code begins to break.
– user9812968
Nov 10 at 22:42
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You can create a Map of the objects using the zorder values as keys before sorting zorder.
Then Array#sort() zorder and Array#map sorted array returning the associated object from the Map object
let objects = [{id:100}, {id:300}, {id:400}, {id:200}],
zorder = [1, 3, 4, 2],
map = zorder.reduce((a, c, i) => a.set(c, objects[i]), new Map);
zorder.sort((a, b) => a - b);
objects = zorder.map(n => map.get(n));
console.log('zorder', zorder)
console.log('objects', objects)
yes that works well but do the id's just go into my already existing objects, and also do the id's have to be in the same order as the zorder to begin with. otherwise thank you very much.
– user9812968
Nov 10 at 22:52
I only usedid
to give a visual to match zorder. What is actually in each object is totally irrelevant but the new order will be correct
– charlietfl
Nov 10 at 22:53
Notice that I never reference theid
property anywhere
– charlietfl
Nov 10 at 22:55
how would i differentiate the 2 if their z order is the same, for example {id:100}, {id:100} and 1, 1 - at the moment it overrides the second one with the first one regardless, do i have to add an if statement to prevent it or is there a way to do it within the existing code
– user9812968
Nov 10 at 23:19
I assumed that thezorder
was all incremental and unique as in the example in question. If there are duplicates that complicates this a bit
– charlietfl
Nov 10 at 23:21
|
show 7 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can create a Map of the objects using the zorder values as keys before sorting zorder.
Then Array#sort() zorder and Array#map sorted array returning the associated object from the Map object
let objects = [{id:100}, {id:300}, {id:400}, {id:200}],
zorder = [1, 3, 4, 2],
map = zorder.reduce((a, c, i) => a.set(c, objects[i]), new Map);
zorder.sort((a, b) => a - b);
objects = zorder.map(n => map.get(n));
console.log('zorder', zorder)
console.log('objects', objects)
yes that works well but do the id's just go into my already existing objects, and also do the id's have to be in the same order as the zorder to begin with. otherwise thank you very much.
– user9812968
Nov 10 at 22:52
I only usedid
to give a visual to match zorder. What is actually in each object is totally irrelevant but the new order will be correct
– charlietfl
Nov 10 at 22:53
Notice that I never reference theid
property anywhere
– charlietfl
Nov 10 at 22:55
how would i differentiate the 2 if their z order is the same, for example {id:100}, {id:100} and 1, 1 - at the moment it overrides the second one with the first one regardless, do i have to add an if statement to prevent it or is there a way to do it within the existing code
– user9812968
Nov 10 at 23:19
I assumed that thezorder
was all incremental and unique as in the example in question. If there are duplicates that complicates this a bit
– charlietfl
Nov 10 at 23:21
|
show 7 more comments
up vote
0
down vote
accepted
You can create a Map of the objects using the zorder values as keys before sorting zorder.
Then Array#sort() zorder and Array#map sorted array returning the associated object from the Map object
let objects = [{id:100}, {id:300}, {id:400}, {id:200}],
zorder = [1, 3, 4, 2],
map = zorder.reduce((a, c, i) => a.set(c, objects[i]), new Map);
zorder.sort((a, b) => a - b);
objects = zorder.map(n => map.get(n));
console.log('zorder', zorder)
console.log('objects', objects)
yes that works well but do the id's just go into my already existing objects, and also do the id's have to be in the same order as the zorder to begin with. otherwise thank you very much.
– user9812968
Nov 10 at 22:52
I only usedid
to give a visual to match zorder. What is actually in each object is totally irrelevant but the new order will be correct
– charlietfl
Nov 10 at 22:53
Notice that I never reference theid
property anywhere
– charlietfl
Nov 10 at 22:55
how would i differentiate the 2 if their z order is the same, for example {id:100}, {id:100} and 1, 1 - at the moment it overrides the second one with the first one regardless, do i have to add an if statement to prevent it or is there a way to do it within the existing code
– user9812968
Nov 10 at 23:19
I assumed that thezorder
was all incremental and unique as in the example in question. If there are duplicates that complicates this a bit
– charlietfl
Nov 10 at 23:21
|
show 7 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can create a Map of the objects using the zorder values as keys before sorting zorder.
Then Array#sort() zorder and Array#map sorted array returning the associated object from the Map object
let objects = [{id:100}, {id:300}, {id:400}, {id:200}],
zorder = [1, 3, 4, 2],
map = zorder.reduce((a, c, i) => a.set(c, objects[i]), new Map);
zorder.sort((a, b) => a - b);
objects = zorder.map(n => map.get(n));
console.log('zorder', zorder)
console.log('objects', objects)
You can create a Map of the objects using the zorder values as keys before sorting zorder.
Then Array#sort() zorder and Array#map sorted array returning the associated object from the Map object
let objects = [{id:100}, {id:300}, {id:400}, {id:200}],
zorder = [1, 3, 4, 2],
map = zorder.reduce((a, c, i) => a.set(c, objects[i]), new Map);
zorder.sort((a, b) => a - b);
objects = zorder.map(n => map.get(n));
console.log('zorder', zorder)
console.log('objects', objects)
let objects = [{id:100}, {id:300}, {id:400}, {id:200}],
zorder = [1, 3, 4, 2],
map = zorder.reduce((a, c, i) => a.set(c, objects[i]), new Map);
zorder.sort((a, b) => a - b);
objects = zorder.map(n => map.get(n));
console.log('zorder', zorder)
console.log('objects', objects)
let objects = [{id:100}, {id:300}, {id:400}, {id:200}],
zorder = [1, 3, 4, 2],
map = zorder.reduce((a, c, i) => a.set(c, objects[i]), new Map);
zorder.sort((a, b) => a - b);
objects = zorder.map(n => map.get(n));
console.log('zorder', zorder)
console.log('objects', objects)
edited Nov 10 at 23:02
answered Nov 10 at 22:31
charlietfl
137k1285118
137k1285118
yes that works well but do the id's just go into my already existing objects, and also do the id's have to be in the same order as the zorder to begin with. otherwise thank you very much.
– user9812968
Nov 10 at 22:52
I only usedid
to give a visual to match zorder. What is actually in each object is totally irrelevant but the new order will be correct
– charlietfl
Nov 10 at 22:53
Notice that I never reference theid
property anywhere
– charlietfl
Nov 10 at 22:55
how would i differentiate the 2 if their z order is the same, for example {id:100}, {id:100} and 1, 1 - at the moment it overrides the second one with the first one regardless, do i have to add an if statement to prevent it or is there a way to do it within the existing code
– user9812968
Nov 10 at 23:19
I assumed that thezorder
was all incremental and unique as in the example in question. If there are duplicates that complicates this a bit
– charlietfl
Nov 10 at 23:21
|
show 7 more comments
yes that works well but do the id's just go into my already existing objects, and also do the id's have to be in the same order as the zorder to begin with. otherwise thank you very much.
– user9812968
Nov 10 at 22:52
I only usedid
to give a visual to match zorder. What is actually in each object is totally irrelevant but the new order will be correct
– charlietfl
Nov 10 at 22:53
Notice that I never reference theid
property anywhere
– charlietfl
Nov 10 at 22:55
how would i differentiate the 2 if their z order is the same, for example {id:100}, {id:100} and 1, 1 - at the moment it overrides the second one with the first one regardless, do i have to add an if statement to prevent it or is there a way to do it within the existing code
– user9812968
Nov 10 at 23:19
I assumed that thezorder
was all incremental and unique as in the example in question. If there are duplicates that complicates this a bit
– charlietfl
Nov 10 at 23:21
yes that works well but do the id's just go into my already existing objects, and also do the id's have to be in the same order as the zorder to begin with. otherwise thank you very much.
– user9812968
Nov 10 at 22:52
yes that works well but do the id's just go into my already existing objects, and also do the id's have to be in the same order as the zorder to begin with. otherwise thank you very much.
– user9812968
Nov 10 at 22:52
I only used
id
to give a visual to match zorder. What is actually in each object is totally irrelevant but the new order will be correct– charlietfl
Nov 10 at 22:53
I only used
id
to give a visual to match zorder. What is actually in each object is totally irrelevant but the new order will be correct– charlietfl
Nov 10 at 22:53
Notice that I never reference the
id
property anywhere– charlietfl
Nov 10 at 22:55
Notice that I never reference the
id
property anywhere– charlietfl
Nov 10 at 22:55
how would i differentiate the 2 if their z order is the same, for example {id:100}, {id:100} and 1, 1 - at the moment it overrides the second one with the first one regardless, do i have to add an if statement to prevent it or is there a way to do it within the existing code
– user9812968
Nov 10 at 23:19
how would i differentiate the 2 if their z order is the same, for example {id:100}, {id:100} and 1, 1 - at the moment it overrides the second one with the first one regardless, do i have to add an if statement to prevent it or is there a way to do it within the existing code
– user9812968
Nov 10 at 23:19
I assumed that the
zorder
was all incremental and unique as in the example in question. If there are duplicates that complicates this a bit– charlietfl
Nov 10 at 23:21
I assumed that the
zorder
was all incremental and unique as in the example in question. If there are duplicates that complicates this a bit– charlietfl
Nov 10 at 23:21
|
show 7 more comments
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%2f53243950%2fsorting-an-array-in-the-same-order-as-another-one%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
because the objects array needs to stay exactly the same except for the order
Do you mean that reassignment (or extracting into a new variable) is not an option?– CertainPerformance
Nov 10 at 22:15
What are obj1, obj2, obj3, and so on? Strings?
– Ele
Nov 10 at 22:16
the object array is full of objects
– user9812968
Nov 10 at 22:32
How do you identify objects using the values within the array
zorder
?– Ele
Nov 10 at 22:34
also yes reassignment isn't an option, the objects can only exist inside the original array or the rest of my code begins to break.
– user9812968
Nov 10 at 22:42