# Array of Structures: Sorting

This is a discussion on Array of Structures: Sorting within the C Programming forums, part of the General Programming Boards category; Hi! Ok, I honestly have exausted my mind over this one. I have an array of 12 structures, each one ...

1. ## Array of Structures: Sorting

Hi!

Ok, I honestly have exausted my mind over this one.

I have an array of 12 structures, each one has a name and several other double variables.

I am trying to sort the entries in the structure based on the value of one of those double variables. The one with the largest value ending up at the top.

If anyone has any suggestions for how I could go about it, im not looking for a code reply or 100% but more of a hard shove in the right direction

Currently I have defined a structure called holder to be a temporary place to move a position in the array of structures to.
I have also made an array of 1 of this structure so I can simply refer to it as holder[pos].name, .p etc.

just to explain before the code, .name is a character array[20].
.t is a double. This is the value I want to sort by.

holders is the array of holder structure, my temporary structure.

cars is the main structure I want to sort, cars is an array[12] of car structures.

Code:
```int pos = 0;
int i;

for(i=0;i<12;i++)
{
if(cars[i].t>cars[i+1].t)
{

strcpy(holders[pos].name, cars[i].name);
holders[pos].t = cars[i].t;

strcpy(cars[i].name, cars[i+1].name);
carss[i].t = cars[i+1].t;

strcpy(cars[i+1].name, holders[pos].name);
cars[i+1].t = holders[pos].t;

}
}
}```
thanks

2. Try looking up a sorting algorithm if you haven't done so already. Quicksort is generally an easy to implement sorting algorithm thats pretty fast as long as you choose the correct pivot point.

http://www.cprogramming.com/tutorial...quicksort.html

3. more over trying to swap each data members of the struct separate which is not required
ckeck out the following prog

Code:
```#include<stdio.h>

struct abc
{
char name[10];
int i;
float f;
};

int main()
{
struct abc a={"hello",2,10};
struct abc b;

b=a;

printf("%s",b.name);
getchar();

}```
Code:
```outpout: -
hello```
u can see that by using just an assigment operator to assign one struct a to struct b
Code:
`b=a;`
s.s.harish

4. Sorted! (get it? sorted...)
Cheers

Popular pages Recent additions