# Sorting 2.. 1-D arrays of different types

• 04-04-2002
GaGi
Sorting 2.. 1-D arrays of different types
Working on a program that reads in a .txt file with a list of stocks and stock data i.e...
Co. LO HI
------------------
AOL 40 48
HP 23 45
CSCO 18 60

I've created 2 separte arrays one the ticker and one for the data.
I need to sort the ticker array while also sorting the corresponding hi and low data. Not sure how being that they are of different type. Thanks for any assistance.
• 04-04-2002
Unregistered
sort
Hi,
I'd suggest a struct

struct stock
{
char name[10] ; //Symbols are shorter than 9 chars
float low;
float high;
};

struct stock Mystocks[100]; // 100 stocks

Use strcmp() to put them in order(ie.)

struct stock TEMP;

if(strcmp(Mystock[x].name, Mystock[x+1].name) > 0)
{
TEMP = Mystock[x];
etc.
}

That way all three struct members are changed at once.

Hope this helps.

Pappy
• 04-04-2002
Prelude
>I need to sort the ticker array while also sorting the
>corresponding hi and low data
The easiest and most useful way is to simply have different sorting options, one for alphabetical sort, one for high sort, and one for low sort. Another possible solution is to hash all of the values together and use that as the condition for swapping in the sort:
Code:

```/* Very crude example */ hash1 = hash ( a[j-1].array ), hash2 = hash ( a[j].array ); if ( a[j-1].low + a[j-1].high + hash1 > a[j].low + a[j].high + hash2 ) {   temp  = a[j-1];   a[j-1] = a[j];   a[j]  = temp; }```
The problem with this is that it may not sort it quite as you want, which is a reason to sort for each field instead of combining all of the fields. The output of this sort test with your given data is:
Code:

```HP  23 45 AOL  40 48 CSCO 18 60```
-Prelude
• 04-04-2002
Bull
the quickest and most efficient way if your NOT using dynamic memory is to read in the entire file first then sort it (guessing you already knew this but just incase), otherwise if you sort on each insertion you will be doing a hell of a lot of sorting when it can be quickly done at the end.