Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Sorting a linked list

This topic is 6334 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I have a array of 8 NPCs sructers. Each is linked to the next NPC and to the last NPC by a int. But i am having truble with my sorting. I wanted to sort them by there y values, in ascending order. But it just will not work. here is my code for sorting: struct NPCS { int nextNpc, lastNpc; RECT npcDestination; // location on the map } struct NPC { int numNPCs; // this equals 8 NPCS npcs[8]; }; NPC npc; int snlTemp = -1; // SortNpcListTemp // Initialize them for (int outer=0;outer<(npc.numNPCs-1);outer++) { for (int inner=outer;innernpc.npcs[inner].npcDestination.top) { // Update the lastNpc to point to the other Npc if (npc.npcs[outer].lastNpc!=-1) { npc.npcs[npc.npcs[outer].lastNpc].nextNpc = inner; } if (npc.npcs[inner].lastNpc!=-1) { npc.npcs[npc.npcs[inner].lastNpc].nextNpc = outer; } // Switch the lastNpc snlTemp = npc.npcs[inner].lastNpc; npc.npcs[inner].lastNpc = npc.npcs[outer].lastNpc; npc.npcs[outer].lastNpc = snlTemp; // Update the nextNpc to point to the other Npc if (npc.npcs[outer].nextNpc!=-1) { npc.npcs[npc.npcs[outer].nextNpc].lastNpc = inner; } if (npc.npcs[inner].nextNpc!=-1) { npc.npcs[npc.npcs[inner].nextNpc].lastNpc = outer; } // Switch the nextNpc snlTemp = npc.npcs[inner].nextNpc; npc.npcs[inner].nextNpc = npc.npcs[outer].nextNpc; npc.npcs[outer].nextNpc = snlTemp; } } } Well i hope that this gives the general idea. Any way here are my 8 NPCs and there values, npcDestination.top is there y value: npc.npcs[0].npcDestination.top = 20 npc.npcs[1].npcDestination.top = 20 npc.npcs[2].npcDestination.top = 750 npc.npcs[3].npcDestination.top = 500 npc.npcs[4].npcDestination.top = 500 npc.npcs[5].npcDestination.top = 70 npc.npcs[6].npcDestination.top = 20 npc.npcs[7].npcDestination.top = 900 That is there y values, now this is how my code sorts them: npcs[0] npcs[1] npcs[4] npcs[5] npcs[6] npcs[3] npcs[2] npcs[7] When the correct sort is: npcs[0] npcs[1] npcs[6] npcs[3] npcs[4] npcs[5] npcs[2] npcs[7] Only the middle 4 are wrong. I can''t figure our why, can anyone help???

#### Share this post

##### Share on other sites
Advertisement
Man, alot of people posted to my question! Well, i guess i have to thank you all who posted to my problem, because i fixed it, thanks!

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
65
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 18
• 10
• 30
• 20
• 9
• ### Forum Statistics

• Total Topics
633416
• Total Posts
3011771
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!