Bubble sort 2 corresponding array


Just wondering, how can I bubble sort 2 corresponding array?
For example, I have an array of string called Items and array of float called price and these arrays are related to each other (i.e. items[0] relates to price[0]). I want to sort the item based on the price from lowest to highest and both arrays need to still be aligned to each other after the search

Thanks in Advance
Just implement the bubble sort as normal but when you swap two items in the array, do the same, at the same position, in the other array.
Last edited on
If you find yourself trying to maintain several parallel arrays, it's usually a sign that what you really want is a struct, or a class, which combines those things into a single type, e.g.

struct Item
  std::string itemName;
  float       price;

Then you can have a single array of structs:

Item items[NUM_ITEMS];

You can then sort the array based on the price, without having to worry about also updating another array to match the new order.
Last edited on
std::map<float,string> will help you to get it done easily.

myMap<price, name> is the easiest way!!
Topic archived. No new replies allowed.