Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More on sorting a struct 1

Status
Not open for further replies.

ecannizzo

Programmer
Sep 19, 2000
213
US
I revamped the code and now I'm not getting errors, but it still won't work. On my line where I say partid1 > partid2, partid1 = PS1115 and partid2 = YY7777 and still it says that partid1 is greater than partid2. Here is my code.

Code:
void sortindex(int index_c)
{
	int i, pass, swap;
	char hold[7];
	char partid1[7];
	char partid2[7];
	long recoffset1;
	long recoffset2;
	long holdrecoffset;

	for (pass = 1; pass <= index_c; pass++) {
		swap = 0;
		for (i = 0; i < index_c - pass; i++) {
			strcpy(partid1, indxfrec_t[i].partid);
			strcpy(partid2, indxfrec_t[i + 1].partid);
			recoffset1 = indxfrec_t[i].recoffset;
			recoffset2 = indxfrec_t[i + 1].recoffset;
			if (partid1 > partid2) {
				swap = 1;
				strcpy(hold, indxfrec_t[i].partid);
				strcpy(indxfrec_t[i].partid, indxfrec_t[i + 1].partid);
				strcpy(indxfrec_t[i + 1].partid, hold);
				holdrecoffset = recoffset1;
				indxfrec_t[i].recoffset = indxfrec_t[i + 1].recoffset;
				indxfrec_t[i + 1].recoffset = holdrecoffset;
			}
		}
		if (!swap)
			break;
	}
	for (i = 0; i < index_c; i++)
		printf(&quot;\n%s\t%ld&quot;, indxfrec_t[i].partid, indxfrec_t[i].recoffset);
}

Help...
 
partid is of type char*

you should use strcmp which returns -1 is string 1 < string 2 0 if they are = and +1 if string 1 > string 2

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top