Sort 2-d arrays

This is a discussion on Sort 2-d arrays within the C++ Programming forums, part of the General Programming Boards category; I ve got a 2-D array that looks as such 23 hey 34 are 29 how 38 you 26 there ...

  1. #1
    Unregistered
    Guest

    Sort 2-d arrays

    I ve got a 2-D array that looks as such

    23 hey
    34 are
    29 how
    38 you
    26 there

    How would I sort the first column of integers while also bringing over the strings so that I get

    23 hey
    26 there
    29 how
    34 are
    28 you


    Thanks

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It looks like a 1D array to me. Perhaps you mean a parrallel array?

    Sort the array of integers. Everytime you copy something from n to m, copy the corresponding parrallel array index from n to m.

    Better yet, use a struct with an int and a string, and just sort based on the integer part.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    I've sorted them using string compare but that is sorting by string while I'm wanting a sort by integer. Can you elaborate on your response. Thanks

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    23
    Originally posted by Unregistered
    I've sorted them using string compare but that is sorting by string while I'm wanting a sort by integer. Can you elaborate on your response. Thanks
    I think he means something like this:

    Code:
    struct blah {
    int number;
    string stuff;
    }
    
    sorting algorithm(blah a[])
    {
      for(int i = 0 ; i < range-1; i++) {
      ....
      ....
      ....
    
      //assuming a[i+1].number < a[i].number
      tempstring = a[i+1].stuff;
      tempnumber = a[i+1].number;
      
      a[i+1].stuff = a[i].stuff;
      a[i+1].number = a[i].number;
      a[i].stuff = tempstring;
      a[i].number = tempnumber;
      }
    }
    You can use various sorting algorithms for that but essentially the idea is the same. You can however create two seperate arrays but, again it would be done the same way. Using a struct or even a class just simplifies everything. You shouldn't be able to make it a 2 dimensional array since 1 side is an integer and the other side is a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Using Vectors (cont) - Sort Problem
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2007, 06:31 AM
  3. Merge sort (sorted arrays)
    By myle in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2007, 02:48 AM
  4. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM
  5. using arrays to sort numbers
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:14 PM

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