Solved

sorting arrays...

Posted on 2001-08-08
6
205 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now