How do I sort an array from highest value in the object to lowest value?

I’m trying to sort an array, essentially each item is a vehicle, they each have a score, and I want to sort it from Highest to lowest. I’ve tried a lot but it usually ends up broken and not working. My current system to find 1st place through 4th works great, its just I need to find the players spot when the player is behind 4th and will be shown on the leaderboard. Any help would be greatly appreciated.

I’ll try to come up with a solution I can show you, but I would like to start with a couple questions. It’s pretty late where I am so I’m not opening Unreal at the moment but I’d still like to at least try helping out.

  • What type of property is your score? I assume it would be an integer but I don’t exactly know how your score system works.

With that alone we could figure something out. You’d basically do something like this:

  • Create an array of integers/floats/whatever it may be from your vehicles. Call it something like “ScoresArrayUnsorted”.
  • Create a loop - I’d do a while loop.
    C++: while(!ScoresArrayUnsorted.IsEmpty())
    Blueprints: ScoresArrayUnsorted → Is Empty → NOT Node → While Loop Node
  • Within While loop body: Find the largest int in that array, using a function called Max of Int Array or another method you may prefer, and add it to a fresh new integer array, called ScoresArraySorted. Then, delete the value from ScoresArrayUnsorted.
  • Continue to loop over each score. Eventually you’ll burn through all your scores, the array will be empty, and the While() loop will exit.
  • Now you ScoresArraySorted, which will have all of your scores sorted from greatest to least, starting at Index 0 (greatest). From there, do what you need to do. Call a custom event or a function like OnScoresArraySorted that takes an array of integers as your scores, and do what you need with it.

With that said there’s other things to consider. You’re working with Android, I see from tags. Performance is important, but with mobile, performance is extra important, even with how powerful modern phones are. Never know if someone is gonna try to run your game on their old phone just barely able to meet minimum specs to run an Unreal title.

  • How often do you need to get the score? Do you want to get it and display it every frame, or only at the end of a race, or at some other point during the game? (I assume it’s a race. Could be anything else though. Stunts? Time attack? Causing chaos? Running over things?)
  • Are you displaying the score in a widget?
  • Is this multiplayer, or single-player with AI opponents, or multiplayer with AI opponents, or whatever other thing? In the case that this is multiplayer, you have to consider who’s going to do the sorting of scores, based on if the scores affect gameplay. Is the server going to sort, or will that be the clients problem?

All things that I would consider. But the overall idea from the first half of this response may be enough.

Well, I stay up too late sometimes.

Here’s your Blueprint. I was going to give you the C++ syntax equivalent in case you wanted to transfer it over at any point later down your production pipeline (for aforementioned performance reasons if you run this every frame) but C++ doesn’t get the Max of Int Array function. I could probably locate it in the source code and copy its syntax but I can’t be bothered at the moment.

Local variables:
image

Function inside a graph:
image

This version would only run on the server and send results that you could then bounce to the clients. If you don’t need multiplayer support just trash the SwitchHasAuthority() node. Make sure though, if you’re doing this in multiplayer have a sort of “passthrough event.” For example, an event:

  • Titled: AuthSortScoreArray
  • RPC: Server
  • Reliable: Unreliable RPC if you’re running this often like on a tick. Reliable RPC if you don’t hit this often AND if it’s required for gameplay-impacting stuff.
  • Create an Event Dispatcher that will take your array of sorted scores, and bind it wherever you need it - for example UI/widgets. In hindsight, I should have included that at the end already.

So here’s what’s going on:

  • Enter the function.
  • Local boolean variable: bSuccessful. Avoids having multiple Return nodes. Default false, always assume failure.
  • Switch on Has Auth. For ensuring we’re the server. Always auth in a singleplayer game.
  • Enter For Each loop. Runs through each vehicle, gets its score, and adds it to local array LocScoresUnsorted.
  • Loop Complete: Enter While loop. Runs while the Length of LocScoresUnsorted is greater than 0, i.e. loops until the array is empty.
  • While loop. Gets max int from array, adds to new array LocScoresSorted and then removes it from LocScoresUnsorted. On the next loop body call, there will be a different max int because the last one was removed.
  • Loop Complete. If LocScoresSorted is not empty, set bSuccessful to true.
  • Return. Outputs results, and as a local function none of those local variables will be saved - they’re destroyed and reset when we exit.

I’m sure you know how much of this works but I don’t think it ever killed anyone to have an explanation of what’s going on.

Cheers

This topic has been moved from International to Programming & Scripting.

When posting, please review the categories to ensure your topic is posted in the most relevant space.

Hopefully, this category change will help others with similar questions. In the meantime, good luck and happy developing! :slight_smile:

1 Like