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.










share|improve this question























  • 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














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.










share|improve this question























  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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















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












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)








share|improve this answer






















  • 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











  • 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











  • 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











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',
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
);



);













 

draft saved


draft discarded


















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

























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)








share|improve this answer






















  • 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











  • 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











  • 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















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)








share|improve this answer






















  • 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











  • 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











  • 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













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)








share|improve this answer














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)






share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • 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

















  • 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











  • 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











  • 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
















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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto