How to sort an array alphabetically?
I have an inventory system in the game that I'm creating, and it works rather well. For sake of organization, I'd like to know how I'd be able to better organize it, as items are simply added to an array and displayed in the order that they're picked up. Each actor has different properties such as an item name, weight, if it can be eaten or not, etc.
What I'd like to know, is how I can pull each of the actors from the array, get their name, organize them alphabetically, and then put the actors back in the array with that new order, A through Z.
Any assistance is greatly appreciated! Thank you!
asked Mar 10 '16 at 04:30 PM in Blueprint Scripting
currently, blueprint doesn't have anything for sorting arrays, and sorting alphabetically would be expensive and difficult in blueprints. i think the easiest way to solve your specific problem, is to put all of your game's items in a spreadsheet, and sort that spreadsheet in Excel or Open Office. one of the columns of the spreadsheet could be a Priority integer, which can start at 0 and increment for each row. then, in your game, you can sort your inventory by Priority, instead of ripping apart strings to compare letters.
then you can loop through your items, ripping out the Priority value, and putting it into an array of Ints.
use MinOfIntArray or MaxOfIntArray to create a new temporary array of ints, as well as a new temporary array of your Item structs. every time you copy and delete an entry from the list of ints, do the same to the corresponding element in the list of structs.
sorting strings and ints should be a built in engine feature in my opinion, and i have no idea why it doesn't already exist. it would be nice if blueprint allowed you to override comparison operators, but you need to use C++ for that. currently, strings can be compared for equality, but there is no greater than or less than operators, and there is no MaxOfStringArray/MinOfStringArray, which would also help.
I know, this is an old post but I find a better solution in blueprint and I post it here if someone need a better solution : you can use the node "Get Character as Number" to connvert, like the node say, a character in a number that you can compare and sort.
answered Sep 01 '17 at 10:37 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here