Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Sorting Arrays

Status
Not open for further replies.

timtrust123

Programmer
Dec 19, 2003
19
GB
I have an array with 5 dimension the first 3 are string values and the forth is a numerical value. I need to sort this array by the first , second , third and finaly the fourth dimension.

eg
"FX","USD","NGOVT",2
"FX","USD","NGOVT",1

This should be coming out with the second row first then the first row.

Please let me know if you have anything that can do this.

 
I, at least, am not going to do this for you. I will provide a minimal outline of a suggestion:

Each sort, for the array dimension is the same. There are NUMEROOUS references to sorting within these fora (e.g. e.g. Tek-Tips) as well as other places, so the actual sort method may be found elsewhere, and will not be belaboured here,

Sort the first dimension (of Interest),

Sort the second dimension for the spans of the first dinension which in invariant.

Contimue the process, incrementing each dimension in turn until the last dimension (of interest) is sorted.

Find here in (Tek-Tips) any number of custom sorting routines, several or more of which deal with the sorting of an array on more than a simgle dimension. Study it inm light of the above outline for some few specific procedural insights.

I like to use the "Heap Sort" for multiple dimension sorts (at least beyond the second), using each dimension as a heap set, however this can be painfully slow, particularly if you end up with I/O required for the various heaps. If you have 'unlimited' (REAL) memory it can at least be quite rational.




MichaelRed
mlred@verizon.net

 
The best solution would be a database. Then you could sort by as many criteria as you'd like. Anytime I go over 2 levels of complexity in a data structure, I always give a hard look at using a db to provide the data structure instead.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I would sort on the least important 'column' first. For example, lets say you have 2 columns of data in Excel and you want to sort on A first then on B; in code I would sort on B first then on A. If you do it the other way around and sort on A first (in code) then B, you will mess up the ordering of A when you sort on B.

I hope that makes sense.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top