How do I sort by the second column in an array?

0 favourites
  • 3 posts
From the Asset Store
amazing template of the game of ordering colors with strings made with construct3 (requires c3 license) optimized with j
  • So I have this array. It grabs the UID of an enemy in a specific range, then uses that UID to find the distance between the enemy and the player. Now, I want to sort the data by the distance so that I can grab which enemy is closest to player for further use. I don't think I'd be able to sort by the second column, but I can't figure out how to edit the code so that the distance comes in first before the UID, as the UID is used to grab the distance and assign it to the correct row. Is there any other way to grab the lowest number in a series of columns?

    Here's the code

    Thanks for the help.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • allowiscous

    I love arrays, and I use them all the time. When I need to sort complex data I use a second array to create an index back to the first array.

    But in your case, I am not sure an array is the best way to do this. You already identify the baddies you are interested in with the in_range instance variable, so they are very easy to select. Keeping the IDs in an array doesn't add much benefit when you can just pick them.

    I would add another instance variable to baddies called Distance, and then in the update position section calculate the distance and store it in the instance variable. This will simplify your code, which makes it much easier to read months later when you are trying to remember what you were trying to do...

    after the distance has been updated, you can pick the baddies in_range, and order them by Distance.

  • Thank you, that makes sense. See, I was proud of my code and didn't want to ditch it, but this does make sense. I assume I can use a for (ordered) loop to get the lowest number? Then I can assign order with another instance variable, perhaps...

    Ah well, I think I can work from here to get the desired affect. Thanks.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)