CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

sorting 2 dimensional array (SOLVED)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

sorting 2 dimensional array (SOLVED)
PostPosted: Mon Jan 24, 2011 9:04 pm     Reply with quote

Hey guys, (don't laugh)

I have 2 arrays (that are lined up with [n] (by row))...

array1[n] has values I'd like to sort.
array2[n] has values I'd like to sort next.

Input would look like.

Code:

      Array1     Array2
[0]     1             3
[1]     2             4
[2]     0             1
[3]     1             5
[4]     0             9


I'd like the output to look like:

Code:

      Array1     Array2
[0]     0             1
[1]     0             9
[2]     1             3
[3]     1             5
[4]     2             4


Is qsort good for this?

Thanks,

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D


Last edited by bkamen on Tue Jan 25, 2011 12:17 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jan 24, 2011 9:30 pm     Reply with quote

Aw, why not 'learn by doing'?
Can't take more than 1/2 hour to prove it yourself!

If it doesn't work for you, the good old bubble sort will !
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Jan 24, 2011 9:55 pm     Reply with quote

I suppose I will if PCM or TTelmah don't have a quick "best practices" guide..

The net is littered with "2 dimensional" sort examples, but not 2 dimensional with a double sort. So I've basically been getting googled with not-so-helpful search results.

So I thought I'd ask some of the pro's here.

In the meanwhile, I'll grind away something that works.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Jan 25, 2011 12:16 am     Reply with quote

Ok, well, I got that figured out.

Thanks for all the suggestions... Hahahahah..

(I'm j/k)

THIS IS SO COOL!

I ended up writing a FOR loop that filters my 1st column and builds a single dimensional array out of the 2nd/3rd items (which are related) and then runs qsort against those... the results get tossed onto another stack which is then built up from the FOR loop.

When the loop end, everything is copied back into the original 3 dimensional array... and PRESTO!

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jan 25, 2011 7:16 am     Reply with quote

learn by doing !!

it's best way...

the 'trick' is to remember WHICH program had the code that did what you wanted instead of doing it all over !!

also use lots of comments to tell you WHAT you did and WHY !

3 dayze from now, you'll understand why....hahaha
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Jan 25, 2011 9:53 am     Reply with quote

temtronic wrote:
learn by doing !!

it's best way...
Quote:


Yep. You're preaching to the choir on that.


the 'trick' is to remember WHICH program had the code that did what you wanted instead of doing it all over !!

also use lots of comments to tell you WHAT you did and WHY !

3 dayze from now, you'll understand why....hahaha


I understand what I did now. The problem with all the links on the web were they were horribly written... I found a lot of "classroom" examples that the teacher PDF'd for his/her students... and while I was reading, I was like, "Dang, this guy needs to take an English composition class. His writing sucks!"

Also, most of the examples that said "2 dimensional" where actually single dimension sorts of a 2 dimensional array. I needed a 2 dimensional sort of a 2 dimensional array.

But I got google'd to death.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Jan 25, 2011 11:57 am     Reply with quote

I don't see the 2-D aspect. The sort criterion is still creating a 1-D order of record indices, it combines both record fields however, similar to a multi byte compare of larger numeric types. So combine the row elements of both arrays in a record, and sort according a union of both numbers.
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Jan 25, 2011 12:04 pm     Reply with quote

You're right... that's what I ended up doing.

I think my problem initially was how I was thinking about it.

"gee, I have 3 columns to sort"

Then I thought, "I can stick the last 2 cols together making them one" (and I used a union to do that) and "ok, now I can just filter column 1 in ascending order and build a single column array --- which will be easy to sort."

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group