4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

Also, does the entire struct move when you sort it or do you have to move each item of the struct in the array? Thanks.

0

You can sort your array by an one field. Compare the same fields into two objects in the array. And then use Bubble sort, for example.

But, i suggest you to use std::vector for keeping that structures. In this case you need to overload a < operator in your structure. After that just call sort method for your vector.

0

Thanks but no luck. The closest i'm getting is with one error. It doesn't recognize the .state part of populationArray[].state. This is a struct array with elements inside the struct of course. Nothing is recognizing the elements in a sort. Once again, I appreciate your help I'm just a C# programmer not a C++ programmer. Now I've had to switch languages and its throwing me off.

I've tried Bubble Sorts and Selection sorts and even the a/b sort in the previous post to no avail.

Thanks for your efforts

0

You need a compare function and a swap function:

int compare (data item1, data item2)
{
  // returns -1 if item1 is "before" item2
  // returns 0 if item1 and item2 have "equal" value regarding ordering
  // returns 1 if item2 is "before" item1

  // You'll have to decide the criteria for "equal" and "before"
}

void swap (data& item1, data& item2)
{
  // swap values of each struct element of data1 and data2.
}

Then just do a bubble sort on the array like you would with a primitive type like an integer, only use these functions.

0

Thanks but no luck. The closest i'm getting is with one error. It doesn't recognize the .state part of populationArray[].state.

I assume there is an index in there?

data populationArray[5];
int i = 2;
populationArray[].state  = "CA"; // incorrect
populationArray[0].state = "CA";// correct
populationArray[i].state = "CA";// correct
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.