Sorting an array of structures

The problem I am working on requires that the "Customer Records" be stored alphabetically by last name in an array of structures. I have tried using a Bubble Sort and a Selection Sort (shown below) but the SortRecords function does not seem to work. It will move the array elements around, and it sometimes makes them appear in alphabetical order when the whole array is displayed, but if it is called more than once, the elements will be moved around again, seemingly randomly. I need help figuring out why it doesn't properly work.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
struct CustomerAccountRecords
{
	string fullName, lastName;
	string streetAddress, city, state;
	int zipCode = 0,lastPaymentDate = 0;
	long long int phoneNumber = 0;
	double accountBalance = 0;
};

int main()
{
	CustomerAccountRecords *recordPtr = nullptr;
	recordPtr = new CustomerAccountRecords[100];
	int choice = 0, numRecords = 0;
	fstream RecordFile;
	
	do {
		cout << endl;
		choice = Menu();
		switch (choice) {
		case 1: 
			NewCustomerRecord(recordPtr, numRecords);
			numRecords++;
			SortRecords(recordPtr, numRecords);
			break;
		case 2: 
			DisplayCustomerRecord(recordPtr, numRecords);
			break;
		case 3: 
			DeleteCustomerRecord(recordPtr, numRecords);
			numRecords--;
			break;
		case 4: 
			EditCustomerRecord(recordPtr, numRecords);
			SortRecords(recordPtr, numRecords);
			break;
		case 5: 
			SortRecords(recordPtr, numRecords);
			DisplayAllRecords(recordPtr, numRecords);
			break;
		case 6: 
			SortRecords(recordPtr, numRecords);
			SaveCustomerRecords();
			break;
		}
	} while (choice < 6 || choice > 0);

	delete[] recordPtr;
	RecordFile.close();

	return 0;
}

void SortRecords(CustomerAccountRecords* ptr, int SIZE)
{
	int minIndex, size = 100;
	string firstName;

	for (int start = 0; start < size - 1; start++) {
		minIndex = start;
		for (int i = start + 1; i < size; i++) {
			if (ptr[i].lastName < ptr[minIndex].lastName) {
				minIndex = i;
			}
			swap(ptr[minIndex].lastName, ptr[start].lastName);
			swap(ptr[minIndex].accountBalance, ptr[start].accountBalance);
			swap(ptr[minIndex].lastName, ptr[start].lastName);
			swap(ptr[minIndex].city, ptr[start].city);
			swap(ptr[minIndex].fullName, ptr[start].fullName);
			swap(ptr[minIndex].lastPaymentDate, ptr[start].lastPaymentDate);
			swap(ptr[minIndex].phoneNumber, ptr[start].phoneNumber);
			swap(ptr[minIndex].state, ptr[start].state);
			swap(ptr[minIndex].streetAddress, ptr[start].streetAddress);
			swap(ptr[minIndex].zipCode, ptr[start].zipCode);
		}
	}
}
Maybe you should try responding to your posts.
If you ignore us, we will ignore you.
I'm sorry, I didn't mean to ignore you. I'm new here and I thought I marked the last one as resolved. I apologize if I offended you.
If you're free to choose the sorting algorithm, why not just use std::sort?

Otherwise, for selection sort, I'd like to remind you that the inner loop is supposed to find the smallest element in-range, and not actually move anything around.

-Albatross
Thank you! I will try that. We haven't learned about std::sort, only Bubble Sort and Selection Sort.
I moved the 'swaps' to the outside of the second for loop, and as far as I can tell, nothing has changed about how the results move around. Here are the results:


Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
5

Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
1
Enter New Customer Account Information Below:
Full Name (First Last): Lauren Kate
Street Address: 1319
City: Hunt
State: AL
ZIP Code (NNNNN): 35812
Telephone Number (NNNNNNNNNN): 2565098777
Account Balance: $12.34
Date of Last Payment (MMDDYYYY): 02042000

Account Information has been saved.

Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
5

Name: Lauren Kate
Street Address: 1319
City: Hunt
State: AL
ZIP: 35812
Telephone Number: 2565098777
Account Balance: $12.34
Date of Last Payment: 02042000


Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
1
Enter New Customer Account Information Below:
Full Name (First Last): David Dodd
Street Address: 120
City: Mays
State: Al
ZIP Code (NNNNN): 35748
Telephone Number (NNNNNNNNNN): 2567367844
Account Balance: $10000
Date of Last Payment (MMDDYYYY): 02081966

Account Information has been saved.

Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
5

Name: David Dodd
Street Address: 120
City: Mays
State: Al
ZIP: 35748
Telephone Number: 2567367844
Account Balance: $10000.00
Date of Last Payment: 02081966


Name:
Street Address:
City:
State:
ZIP: 00000
Telephone Number: 0000000000
Account Balance: $0.00
Date of Last Payment: 00000000


Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
5

Name: Lauren Kate
Street Address: 1319
City: Hunt
State: AL
ZIP: 35812
Telephone Number: 2565098777
Account Balance: $12.34
Date of Last Payment: 02042000


Name: David Dodd
Street Address: 120
City: Mays
State: Al
ZIP: 35748
Telephone Number: 2567367844
Account Balance: $10000.00
Date of Last Payment: 02081966


Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
5

Name:
Street Address:
City:
State:
ZIP: 00000
Telephone Number: 0000000000
Account Balance: $0.00
Date of Last Payment: 00000000


Name: Lauren Kate
Street Address: 1319
City: Hunt
State: AL
ZIP: 35812
Telephone Number: 2565098777
Account Balance: $12.34
Date of Last Payment: 02042000


Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
5

Name: David Dodd
Street Address: 120
City: Mays
State: Al
ZIP: 35748
Telephone Number: 2567367844
Account Balance: $10000.00
Date of Last Payment: 02081966


Name:
Street Address:
City:
State:
ZIP: 00000
Telephone Number: 0000000000
Account Balance: $0.00
Date of Last Payment: 00000000


Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
5

Name: Lauren Kate
Street Address: 1319
City: Hunt
State: AL
ZIP: 35812
Telephone Number: 2565098777
Account Balance: $12.34
Date of Last Payment: 02042000


Name: David Dodd
Street Address: 120
City: Mays
State: Al
ZIP: 35748
Telephone Number: 2567367844
Account Balance: $10000.00
Date of Last Payment: 02081966


Select a number from the menu below:
1. Create a new customer record?
2. Display an existing customer record?
3. Delete an existing customer record?
4. Edit an existing customer record?
5. Display all existing customer records?
6. Save all existing customer records and exit?
Try this:

1
2
3
4
5
6
7
8
9
10
void SortRecords(CustomerAccountRecords* ptr, int SIZE) {
	for (int start = 0; start < SIZE; start++) {
		int minIndex = start;
		for (int i = start + 1; i < SIZE; i++) {
			if (ptr[i].lastName < ptr[minIndex].lastName)
				minIndex = i;
		}
		std::swap(ptr[minIndex], ptr[start]);
	}
}

That worked! Thank you so much!!! Sorry again for before!
Topic archived. No new replies allowed.