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?

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
#include <iostream>
#include <string>
#include <algorithm>
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.