Solved

sorting arrays...

Posted on 2001-08-08
6
210 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
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!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 76
Best book to learn C++ 4 96
Beginner to Unreal Engine 4 5 119
HTTPSendRequest with WinINet delays on first call 11 119
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

732 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