Link to home
Start Free TrialLog in
Avatar of Ra
Ra

asked on

sorting arrays...

Hey all, I'm trying to find a quick way to sort an array.  This array has one element, a structure, and this structure has two elements.  Something like this:

struct MyStruct{
   int arg1;
   int arg2;
}MS;

struct MS MyArray[50];

Now I want to sort the array based on arg2.  Does anybody know a quick way of doing this.  I've thought about doing a bubble sort, but there has to be a quicker way.  I've tried using qsort, but I can't figure out how to make it look at arg2 as the sort key.

I need the code in C, not C++, and it must be ANSI compatable and able to run in DOS.  I'm using MS VC 1.52c for MSDOS programs.

Thanks.
Avatar of Axter
Axter
Flag of United States of America image

FYI,
You might want to post this type of question in the C topic area, or at least post a link to it.
https://www.experts-exchange.com/jsp/qList.jsp?ta=cprog
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ra
Ra

ASKER

Axter, sorry, didn't realize there was a C topic area. ;)

jkr, thanks.  I'll test it when I get to work tomorrow morning.  I never thought about type casting those args to my structure.  It should work great.  I'll give you the points after I test it.
>>never thought about type casting

In good ol' "C", it's the only way :o)

In C++, using STL would be the idea...
Avatar of Ra

ASKER

Excellent, it works.  I had to change the second line in the compare function, a little copy and paste error there it looks like. ;)

Thanks jkr.
>>a little copy and paste error there it looks like. ;)

Yup, I just saw it :o)