Solved

sorting arrays...

Posted on 2001-08-08
6
204 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

16 Experts available now in Live!

Get 1:1 Help Now