### Sort Structures.

I need to sort a structure by the string in it. my function sort was my attempt but since the sting is part of the class I cant set it equal to jsut plan string. Any thoughts?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include #include using namespace std; const int SIZE = 3; struct part_rec { string partnum; float value; int quantity; }; void deletePart(part_rec arr[], string partnum) { for(int i = 0; i < SIZE; i++) { if(arr[i].partnum == partnum) { for(int r = i; r < SIZE; r++) { arr[r] = arr[r + 1]; } }else if(arr[i].partnum != partnum) { continue; } } } void sort(part_rec arr[]) { string first = " ", temp = " ", int i,j; for (i = 0; i < SIZE; i++) { for (j=1; j<=i; j++) { if (arr[j] < arr[first]) first = j; } temp = arr[first].partnum; arr[first].partnum = arr[i].partnum; arr[i].partnum = temp; } return; } void main() { part_rec arr[SIZE] ={{"5", 10, 2}, {"3", 16, 5}, {"7", 11, 3}}; //sort(arr); cout << arr[0].partnum; cout << arr[1].partnum; cout << arr[2].partnum; system("pause"); }``````
closed account (S6k9GNh0)
http://en.cppreference.com/w/cpp/string/basic_string/compare

Particularly, version 3 4.
Last edited on
I read it and I still have no idea on how to do it. Because the size of the string would be the same so it wouldnt change through that.
Thanks anyways :) I just saw that i set a variable to the wrong type silly me :P
Topic archived. No new replies allowed.