Sorting 2.. 1-D arrays of different types

This is a discussion on Sorting 2.. 1-D arrays of different types within the C Programming forums, part of the General Programming Boards category; Working on a program that reads in a .txt file with a list of stocks and stock data i.e... Co. ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    12

    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.

  2. #2
    Unregistered
    Guest

    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

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. sorting arrays
    By bradleyd in forum C++ Programming
    Replies: 9
    Last Post: 06-17-2008, 02:28 PM
  3. Replies: 6
    Last Post: 11-28-2004, 11:01 AM
  4. Sorting Arrays???
    By gL1tch` in forum C Programming
    Replies: 4
    Last Post: 04-30-2004, 12:16 PM
  5. storing different variable types in arrays.........
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-28-2002, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21