# Thread: Sorting an array of structures with sort()

1. ## Sorting an array of structures with sort()

I want to sort an array of structures based on one of their members. I did this using a sort I wrote, and it works, but I recently discovered that sort() from the standard is about 200 times faster. So I want to sort my stuff with std::sort but I don't know how to use std::sort with an array of structures, sorting based only on a certain member from the structure. I don't even know if it's possible to do this.

Any help would be appreciated, thanks

the code from my slow-slow-slow sort function, if it's of any use to give you the idea:

Code:
```int minVal = 0;
int minPos;
NPC tmp;

for (int u = 0; u < MainMap.numNPCs-1; ++u) {
minPos = -1;
for (int v = u; v < MainMap.numNPCs; ++v) {
if (NPCs[v].aPixY < minVal || minPos == -1) {
minVal = NPCs[v].aPixY;
minPos = v;
}
}
tmp = NPCs[u];
NPCs[u] = NPCs[minPos];
NPCs[minPos] = tmp;
}```

2. Maybe your sort() is so much slower than std::sort() because you're sorting structures and not numbers or characters.

Just a thought.

3. I could be wrong about this but I think if you overload the relational operators (==,>,<,>=,<=) you can use sort. As long as the computer understands what "if (myclass1>myclass2)" means along with the other operators I think sort will work.

If not, let us know and we can help you with creating your own sort which there are plenty of and that will be more efficient than the one you have now.