Solved

sorting arrays...

Posted on 2001-08-08
6
209 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:Ra
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6365947
FYI,
You might want to post this type of question in the C topic area, or at least post a link to it.
http://www.experts-exchange.com/jsp/qList.jsp?ta=cprog
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6365964

>>how to make it look at arg2 as the sort key

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

int compare( const void *arg1, const void *arg2 );

void main( int argc, char **argv )
{

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

struct MS MyArray[50];

//....

   qsort( (void *)MyArray, (size_t)50, sizeof( struct MS ), compare );

}

int compare( const void *arg1, const void *arg2 )
{
struct MS* p1 = (struct MS*) arg1;
struct MS* p1 = (struct MS*) arg1;

if ( p1->arg2 > p2->arg2) return ( 1);
if ( p1->arg2 < p2->arg2) return ( -1);
 
 
 return ( 0);
}
0
 

Author Comment

by:Ra
ID: 6366226
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 6366505
>>never thought about type casting

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

In C++, using STL would be the idea...
0
 

Author Comment

by:Ra
ID: 6367938
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6367957
>>a little copy and paste error there it looks like. ;)

Yup, I just saw it :o)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 70
Why isn't object file created? 6 86
need help ...get stuck at middle of coding ...need to submit by Sunday C++ 1 43
boost::uuid crashes 17 35
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question