Solved

sorting arrays...

Posted on 2001-08-08
6
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

729 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