View previous topic :: View next topic |
Author |
Message |
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
sorting 2 dimensional array (SOLVED) |
Posted: Mon Jan 24, 2011 9:04 pm |
|
|
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: 9243 Location: Greensville,Ontario
|
|
Posted: Mon Jan 24, 2011 9:30 pm |
|
|
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: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Jan 24, 2011 9:55 pm |
|
|
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: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Jan 25, 2011 12:16 am |
|
|
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: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jan 25, 2011 7:16 am |
|
|
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: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Jan 25, 2011 9:53 am |
|
|
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
|
|
Posted: Tue Jan 25, 2011 11:57 am |
|
|
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: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Jan 25, 2011 12:04 pm |
|
|
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 |
|
|
|