string::compare not working?

I don't know if I'm using string::compare right.. I am using insertion sort to sort some records that contain a population, city, and state. Population works fine since it is just comparing the integers. I don't see my error in my while loop, compiles fine, but doesn't sort the records by city.

temp is a temporary pointer to Record* inside data[]
vector<Record*> data;

 
while(j > 0 && (*data[j]->city).compare(*temp->city) > 0)
What do you want to check for? Why not just use ==?
I am sorting the records in order of their city name for this portion.


**********INSERTION SORT**********

Original Data
Vina, California, 237
San Francisco, California, 812826
Santa Fe, New Mexico, 68642

Sorted by POPULATION
CPU time (seconds:nanoseconds): 0:2345 on 3 records
Vina, California, 237
Santa Fe, New Mexico, 68642
San Francisco, California, 812826

Sorted by NAME
CPU time (seconds:nanoseconds): 0:4233 on 3 records
San Francisco, California, 812826
Santa Fe, New Mexico, 68642
Vina, California, 237
std::string can already be compared with operator<
I tried that method.
Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void CensusData::insertionSort(int sortN) {
   if(sortN == 0) {
      //std::cout << "This should be printing in POPULATION!!!!!!!" << std::endl;
      Record* temp;
      for (int i=1; i < getSize(); i++){
         temp = data[i]; 
         int j = i-1;
         while(j > 0 && data[j]->population > temp->population){
            data[j+1] = data[j];
            j = j-1;
         }
         data[j+1] = temp;
      }
   }
   else if(sortN == 1) {
      //std::cout << "This should be printing in NAME!!!!!!!" << std::endl;
      Record* temp;
      for (int i=1; i < getSize(); i++){
         temp = data[i];        
         int j = i-1;
         while(j > 0 && (*data[j]->city) > (*temp->city)){
            data[j+1] = data[j];
            j = j-1;
         }
         data[j+1] = temp;
      }
  }
}


Output:

**********INSERTION SORT**********

Original Data
Vina, California, 237
San Francisco, California, 812826
Santa Fe, New Mexico, 68642

Sorted by POPULATION
CPU time (seconds:nanoseconds): 0:766 on 3 records
Vina, California, 237
Santa Fe, New Mexico, 68642
San Francisco, California, 812826

Sorted by NAME
CPU time (seconds:nanoseconds): 0:1564 on 3 records
Vina, California, 237
San Francisco, California, 812826
Santa Fe, New Mexico, 68642
The output is correct - it is sorted in reverse order as you used operator> instead of operator<
I tried that.. It is really weird,
operator>
returns the above output and
operator<
returns it sorted by POPULATION... lol
I don't believe you.

You're doing lots of crazy stuff with pointers, I would imagine memory corruption might be an issue.
Is Record::city a cstring, std::string or pointer to std::string? The dereference here looks suspicious: (*data[j]->city) > (*temp->city))
Found the error.. I didn't have an = operator after while(j >= ...)
Topic archived. No new replies allowed.