Thread: Sorting "numerical strings"

  1. #1
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428

    Sorting "numerical strings"

    Hey everyone. I'm making a program that inputs a "serial number", and its associated description. Anyways, I write it all to a file (the number and description), then I read it into two arrays, a serial array and a description array (so serial[x]'s description would be desc[x]). Anyways, I was just using a normal quicksort to sort the serials and then whenever one of them got switched, I'd switch its associated description, so the descriptions didnt get mixed up. Anyways, realizing that I would be using "many digit" serial numbers, integers/doubles...etc no longer would do it. I switched all the serial "numerbs" to actually be character arrays, just with only digits, so they could be as long as I need them to. The problem is I'm stuck on how to sort the serial numbers (along with their assiciated descriptions) in numerical order, being that they are now char's. Does anyone know how to fix this? Thanks!
    Last edited by dirkduck; 01-18-2003 at 04:50 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    start off by having one std::vector of structs having a serial number and a discription. Second as long as serial numbers are all of the same length then they will sort correctly, even as chars. If they are of different lengths then the shorter comes first.

    Code:
    struct record {
        std::string sn;
        std::string desc;
        bool operator < (const record & rhs) const {
            if(sn.length() == rhs.sn.length()) return sn < rhs.sn;
            else return (sn.length() < rhs.sn.length());
        }
    }

  3. #3
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Thanks, I'll give that a shot.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I believe you can do the same thing. When you compare two chars with '>' or '<' it compares their ASCII value, which since the ASCII for '1' is less than '2' etc it should sort no differently than if they were all number based variables.

    [edit] dang I'm slow! [/edit]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Something like -

    Code:
    #include <map>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    	map <string,string> serialmap;
    
    	serialmap["1231235"] = "Something";
    	serialmap["211s121"] = "Nothing";
    	serialmap["bjkbkj87"] = "All";
    
    	cout << serialmap["211s121"];
    	
    	return 0;	
    
    }
    Then you don't have to worry about sorting.
    Joe

  6. #6
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    PJYelton:

    I was thinking about that, but I wasnt sure, I was thinking that if one of them was "09999999" and the other was "10000000", then although the latter is larger, the other would have a higher value because of the 9's. Could be wrong though...

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Doesn't it only compare lengths if the two strings are equal up to the end of the shorter one? So that way "1234" comes before "12345" and also comes before "23"?

    And I think your example should still work dirkduck, it just compares the first char, only moves to the next one if the first is equal.

  8. #8
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Ah, got it. It seems to be working now at least, thanks for the help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  3. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM