Sorting 2 arrays of different type

This is a discussion on Sorting 2 arrays of different type 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 arrays of different type

    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.
    Here is what I have thus far

    char *hold
    if(stockname[w] >stockname[w+1])
    {
    hold = stockname[w];
    stockname[w] = stockname[w+1];
    stockname[w+1] = hold;
    }

    how would also sort the low hi data to correspond with the correct ticker.

    Thanks
    this was previously posted int the wrong board

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    24
    it would be alot easer to sort them if they were in a struct or a class so when you sort one array the other array is in the same order, so if you sort it by high the the low and the name go with it. etc. etc

    hope this helps
    good luck
    Skeptic

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    We are not allowed to use structures nor classes.

    Thanks for the assist though

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    //assuming data is held in a 2D array of int of size [w+1][2]
    //where w+1 is a constant.

    int data[w+1][2];
    char *hold;
    int temp[2];

    if(stockname[w] >stockname[w+1])
    {
    hold = stockname[w];
    temp[0] = data[w][0];
    temp[1] = data[w][1];

    stockname[w] = stockname[w+1];
    data[w][0] = data[w+1][0];
    data[w][1] = data[w+1][1];

    stockname[w+1] = hold;
    data[w+1][0] = temp[0];
    data[w+1][1] = temp[1];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. Help with sorting arrays
    By Silence in forum C Programming
    Replies: 5
    Last Post: 05-17-2002, 11:05 AM

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