My attempt of a "bubble sort" only partially works. Say for example if this is my structure declaration:

struct part {
  char name   [NAME_LEN+1];
  char owner  [OWNER_LEN+1];
  char status [STATUS_LEN+1];
  char date   [DATE_LEN+1];
  char renter [RENTER_LEN+1];
} parts [MAX_PARTS], temp [1];

and the following code print:

Piston:Mike:In::
Hook:Mark:In::
Door:Kim:In::
Axel:Matt:In::

for(i=0; i < num_parts; i++)
	printf("%s:%s:%s:%s:%s\n", parts[i].name, parts[i].owner, parts[i].status, parts[i].date, parts[i].renter);

My desired final output would be sorted alphabetically by the first field like this:

Axel:Matt:In::
Door:Kim:In::
Hook:Mark:in::
Piston:Mike:In::

The following code is my attempt at sorting, but instead of my desired output I get:

::::
Axel:Matt:In::
Door:Kim:In::
Hook:Mark:In::

void status (void){
 
  int i;
  int n;
  for (n=0; n < num_parts; n++){
  
  for (i=0; i < num_parts; i++)
	if (strcmp(parts[i].name, parts[i+1].name) > 0) {
	  temp[1]=parts[i];
	  parts[i]=parts[i+1];
	  parts[i+1]=temp[1];
	  
}
printf("\n");
}
  for(i=0; i < num_parts; i++)
	printf("%s:%s:%s:%s:%s\n", parts[i].name, parts[i].owner, parts[i].status, parts[i].date, parts[i].renter);
	
}

From what I can see the sorting is working correctly, except that for the first time the if() statement is iterated, there is something going wrong. In this example, in the first iteration Piston (parts[0].name) and Hook (parts[1].name) are compared. They need to be switched so they are but there is a problem when the Piston line is saved to temp, I think it is being saved saved with blank fields. After this first iteration, it seems everything is working correctly, so I end up with close to what I want, but in the end parts[0] is just a bunch of blanks.

A simpler example is, if the original order is:

Hook:Matt:In::
Door:Mike:In::

the output this code gives is:

::::
Hook:Matt:In::

Can you spot my mistake?

Nevermind. Should have been

if (i=0; i < num_parts - 1; i++)

i found this and thought hoorray - at last something to help me sort my struct arrays cos am quietly going inxane here nothing seems to work the way i figured itwould and have tried lots of different ways and still no result

i want to sort order of banana -should be simple help from wise ones would be useful and appreciated -
it says banana must have class structure -but i thought it lt already had am sooo confused

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;




struct monkeys {char Name[15];  int banana;};
int main()
{
			
	monkeys monkey;
	int y=0;
	int n=2;
	
	for(int y=0; y<n;y++){
			  cout <<"name please ";
			  cin>> monkey.Name ;
			  cout<<"First monkey number";
			  cin>> monkey.banana;
			  y=y+1;
			  }
   void SortBananaOrder(monkeys(int banana[]));
   int z=0;
	for(z=0; z<2;z++ ){
			cout<<monkey[z].Name<<monkey[z].banana; 
		  }
}
		

 void SortBananaOrder(int banana[]) { 
  int i, j, max;
  for (i = 0; i > banana[i]; i++) {
    max = i;
    for (j = (i+1); j < 2; j++) {
      if(banana[j] < banana[max]) {
        max = j;	  
      }
    }
    if (i != max) {
      int swap = banana[i];
      banana[i] =banana[max];
      banana[max] = swap; 
	
    }
  } 
		

}
void SortBananaOrder(monkeys(int banana[]));

This doesn't look right, and plus that is only a prototype. I doesn't look like you are even calling the function. Prototype it like this

void SortBananaOrder(void);

and call it like

SortBananaOrder();

and define it like this, without the int banana[]

void sortBananaOrder (void) {
.
.
.
}

I don't think you need to pass anything to your function. This is how my code looks..

void owner(void);
.
.
.
owner();
.
.
.
void owner (void) {
  int i;
  int n;
for (n=0; n < num_parts; n++){
  
  for (i=0; i < num_parts-1; i++)
    if (strcmp(parts[i].owner, parts[i+1].owner) > 0) {
      temp[1]=parts[i];
      parts[i]=parts[i+1];
      parts[i+1]=temp[1];
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.