Well, with a little help I got started writing my own functions to sort a vector from least to greatest by a member of it's elements.
It doesn't work at all, and I'm totally perplexed as to why not. Before I got any help, It would run through one iteration of the vector (successfuly putting the largest element at the end) but the program would crash. I couldn't figure out why, so I started over from scratch, and came up with these functions. For some reason, no changes are made to the contactList at all.
It compiles successfuly, but no changes are actually made.Code:void listSort(){ //Bubble sortish int biggest; int i; vector<contact>::iterator contactA; vector<contact>::iterator contactB; for (i=0;i!=contactList.size();++i){ biggest=i; for (contactA=getIterator(&contactList[i]);contactA!=contactList.end();++contactA){ if ((contactA->getFirstName())>(contactList[biggest].getFirstName())){ contactB=getIterator(&contactList[biggest]); biggest=intLoc(contactA); } } iteratorSwap(&contactA,&contactB); } return; } vector<contact>::iterator getIterator(contact *matchingContact){ //Returns an iterator to the referenced element. vector<contact>::iterator i; for (i=contactList.begin();i!=contactList.end();++i){ if (i->getContactID()==matchingContact->getContactID()){//If two elements are the same... return (i); } } return(i); } void iteratorSwap(vector<contact>::iterator *a, vector<contact>::iterator *b){//Swaps elements. vector<contact>::iterator c; c=*a; a=b; *b=c; return; } int intLoc(vector<contact>::iterator matchingContact){//Returns the random access value equivalent to the iterator's position in the vector? vector<contact>::iterator i; int placeInVector=0; for (i=contactList.begin();i!=contactList.end();++i){ if(i->getContactID()==matchingContact->getContactID()){ return (placeInVector); } else{ ++placeInVector; } } return (placeInVector);//Shouldn't ever happen if matchignContact is a legit value }
getContactID() is a function that simply returns the contactID (int) of the contact.
getFirstName() is the same thing, but a string.
Thanks.