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.

Sorting Weapons by id AND element....

I am attempting to sort an array of weapons by id AND element. For example, all weapons with an element of 13 would be grouped together in the array, but be sorted by id within that group. Then next would come all the element 14s sorted by id. So, I have this slice of code:

Code:
@data.sort! {|a, b|

        for i in 13..18

          if a.element_set.include?(i)

            item_a = i - 12

          end

          if b.element_set.include?(i)

            item_b = i - 12

          end

        end

        item_a <=> item_b

      }

It sorts fine by element. How would I implement ids into the picture? Thanks in advance.
 
Okay, well this works:

Code:
@data.sort! {|a, b|

        for i in 13..18

          if a.element_set.include?(i)

            item_a = (i - 12) * 300

            item_a += a.id

          end

          if b.element_set.include?(i)

            item_b = (i - 12) * 300

            item_b += b.id

          end

        end

        item_a <=> item_b

      }

However, something tells me that there is a more "professional" way to do it. Anyone got any ideas/know how?
 

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