Reputation: 19
The problem is if I enter 1, then 2 and 123. It will sort 1,123,2.
How do i make it sort 1,2,123
Item is the Records that was created. Runner is the traverser.
struct Records {
string Name;
string ID;
char Gender;
string Phone_Num;
Records *Next;
};
void SortRecords(Records* Item, Records* head, Records** Set_Head){
Records* Runner = head;
if (head == NULL){
*Set_Head = Item;
}else if (Item->ID<head->ID){
Item->Next=head;
}else{
while(Item->ID>head->ID){
if(Item->ID > Runner->ID && Runner->Next==NULL){
Runner->Next=Item;
break;
}else if(Item->ID<Runner->Next->ID){
Item->Next=Runner->Next;
Runner->Next=Item;
Runner=Item;
cout<<Runner->Next->Name<<endl;
break;
}else{
Runner=Runner->Next;
}
}
}
Upvotes: 1
Views: 110
Reputation: 19041
You can use std::stoi
to compare integers not strings:
std::stoi(Item->ID) < std::stoi(Runner->Next->ID)
You might actually want to store ID as integer, not string.
If you ID can be, say "123AB", then you can extract number with:
string str = "123AB";
size_t last_index = str.find_last_of("0123456789");
string result = str.substr(0, last_index + 1); // result = "123";
Upvotes: 1