Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

the sort command

ive allways wanted to screw around with the sort method to make a script to sort items in my inventory and such. could some one please exsplain to me how to use it properly, because i didnt find the help file all that helpful.

also if its not to much of a hassle is it possible to have an arrray like data = [23,1,2,3,45,....] and have the sort method put the returned array of objects in that order? just in case that makes no sence here is an example:

23= potion, 1 = super potion, 2 =extra kick ass potion, 3= antidote, 45 = super antidote
if you sort it it would be in this order: super potion, extra kick ass potion, antidoe, superantidoe, potion

but if you can control the sort it would be
potion, super potion, extra kick ass potion, antidote, super antidote

the reason i find this would be helpful is if you keep adding itens in your game the would be better fit earlier in the list.
 
returning a sorted array is: Array.sort!
Rewriting class Array's sort method is a bad idea. it'll affect all arrays in the game and will probably mess with other scripts.
yopu can instead use 'sort_by', and give it a block to control the sort.
Try something like:
[rgss]order = {'Platinum'=>4, 'Gold'=>3, 'Silver'=>2, 'Copper'=>1 }
data = ['Platinum', 'Gold', 'Silver', 'Copper']
data = data.sort_by do |item|
         order[item]
       end
[/rgss]
 
thanks ill mess with that when i get home, could you exsplain to me how the wort method works? like the proper forming of the do loop, why you need it. whats with |item| and such, that would be really helpful, like i said i just cant seem to grasp the proper use of sort
 
Well, sort simply.. sorts, but only numbers or letters.*
If your array contains anything else, you can assign each item a number.
That's what the block does- it iterates on the array, and returns an ordinal number for each item.
Your items will be sorted by this number from lowest to highest.
So, the code above returns
[ 'Copper', 'Silver', 'Gold', 'Platinum' ] # the ordinal number is order: [1,2,3,4]

* Actually, that's not accurate. The sort method can also sort any object that has a <=>(other) method defined.
Search up info about it.
 
You could write a custom sort method that is outside the array class, and just manipulates the arrays externally. It's fairly simple, but it requires iterating through the array a bit too much, which will be slow with large arrays.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top