How to manipulate switch function to shorten my code

Each Case the only thing different is 1 word how do I shorten my code so it is not so copy paste?

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
78
79
80
81
82
83
84
85
86
 
void sortBy(Documents *parr, int entry, int &count ) {

	int n = 8;


	switch (entry) {
	case 1: //sorting by name


		bool swapped;
		do
		{
			swapped = false;
			for (int count = 0; count < (n- 1); count++)
			{
				if (parr[count].student1 > parr[count + 1].student1)
				{
					swap(parr[count], j[count + 1]);
					swapped = true;

				}
			}
		} while (swapped);
		break;

	case 2: //sorting by gpa


		do
		{
			swapped = false;
			for (int count = 0; count < (n - 1); count++)
			{
				if (parr[count].gpa > parr[count + 1].gpa)
				{
					swap(parr[count], parr[count + 1]);
					swapped = true;

				}

			}

		} while (swapped);
		break;
	case 3: // sorting by id


		do
		{
			swapped = false;
			for (int count = 0; count < (n - 1); count++)
			{
				if (parr[count].id > parr[count + 1].id)
				{
					swap(parr[count], parr[count + 1]);
					swapped = true;

				}
			}
		} while (swapped);
		break;

	case 4: //sort by email


		do
		{
			swapped = false;
			for (int count = 0; count < (n - 1); count++)
			{
				if (parr[count].email > parr[count + 1].email)
				{
					swap(parr[count], parr[count + 1]);
					swapped = true;
				}

			}
		} while (swapped);
		break;
	}



}
.
Use functions to express the part that changes. Then use an array of function pointers and select the correct one to call.
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
#include <string>
#include <algorithm>
#include <iostream>

using std::string;
using std::cerr;
using std::swap;

struct Documents {  // I created this so the code could compile.
    string student1;
    double gpa;
    unsigned id;
    string email;
};

static bool byName(Documents &left, Documents &right) {
    return (left.student1 > right.student1);
}
static bool byGpa(Documents &left, Documents &right) {
    return (left.gpa > right.gpa);
}
static bool byId(Documents &left, Documents &right) {
    return (left.id > right.id);
}
static bool byEmail(Documents &left, Documents &right) {
    return (left.email > right.email);
}

void
sortBy(Documents * parr, int entry, int &count)
{

    int n = 8;

    // Create an array of function pointers
    bool (*funcs[])(Documents &left, Documents &right) = {
	byName, byGpa, byId, byEmail
    };

    // convert from user friendly values 1-4 to computer friendly
    // values 0-3
    --entry;
    if (entry >= 0 && entry <= 3) {
	bool swapped;
	do {
	    swapped = false;
	    for (int count = 0; count < (n - 1); count++) {
		// call the appropriate function
		if (funcs[entry](parr[count], parr[count + 1])) {
		    swap(parr[count], parr[count + 1]);
		    swapped = true;
		}
	    }
	} while (swapped);
    } else {
	cerr << "error in sortBy(): " << entry << " is a bad value for entry\n";
    }
}

I would love to do that but I can't use more than one function to sort is there anything I can do within the Switch case to make it less copy and paste or no?
Put the switch within the predicate that does the comparison.
if (parr[count].student1 > parr[count + 1].student1)
becomes something like
if ( greatherThan( parr[count], parr[count + 1], entry ) )
with you writing a predicate function
bool greaterThan( Documents doc1, Documents doc2, int entry );
Last edited on
Topic archived. No new replies allowed.