Thread: Sorting string array

  1. #31
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by laserlight View Post
    That would result in a reverse ordered sort.
    I like it! You know, I have been learning more through reading this board, and posting; than I have learned from any book that I have found over the years.

    Thanks!
    Last edited by Phyxashun; 01-06-2009 at 02:14 PM.

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, sorry ah, but I realised that I mixed up the order myself. I should have come up with the edge cases and tested first. A correct version that places empty strings at the end would be:
    Code:
    bool Books::operator<(const Book &obj) const
    {
        if (obj.Author.empty())
        {
            return !Author.empty();
        }
        else if (Author.empty())
        {
            return false;
        }
        return Author < obj.Author;
    }
    EDIT:
    Or slightly more succinctly:
    Code:
    bool Books::operator<(const Book &rhs) const
    {
        if (rhs.Author.empty())
        {
            return !Author.empty();
        }
        return !Author.empty() && Author < rhs.Author;
    }
    Last edited by laserlight; 01-06-2009 at 02:19 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #33
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It appears that the test that puts zero items last needs to be a bit more complicated:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <cstdlib>
    
    class X
    {
        int n;
    public:
        X(): n(rand()%4) {}
        bool operator<(X rhv) const
        {
            if (n == 0) {
                return false;
            }
            else if (rhv.n == 0) {
                return true;
            }
    
            else {
                return n < rhv.n;
            }
        }
        friend std::ostream& operator<<(std::ostream& os, X x)
        {
            return os << x.n;
        }
    };
    
    int main()
    {
        const unsigned max = 80;
        X arr[max] = {};
        std::sort(arr, arr + max);
        std::copy(arr, arr + max, std::ostream_iterator<X>(std::cout, ""));
    }
    Edit: I see you've already noticed that...
    Last edited by anon; 01-06-2009 at 02:28 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #34
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    I was going to also suggest that the books be a vector instead of an array. That would make the sorting easier, and the moving of the empty data to the end easier.

    Would it not?

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anon
    I see you've already noticed that...
    Ah, but your logic is slightly simpler. I should have tried to simplify the logic before trying to write the expression more succinctly. Consequently, we can more easily reduce the expression to:
    Code:
    bool Book::operator<(const Book& rhs) const
    {
        return !Author.empty() && (rhs.Author.empty() || Author < rhs.Author);
    }
    Quote Originally Posted by Phyxashun
    I was going to also suggest that the books be a vector instead of an array. That would make the sorting easier, and the moving of the empty data to the end easier.

    Would it not?
    It would not make the sorting simpler, but there are other advantages, like having a dynamic size and not needing to manually manage memory.
    Last edited by laserlight; 01-06-2009 at 02:49 PM. Reason: Books -> Book
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting 2D string array
    By sureshhewa in forum C Programming
    Replies: 14
    Last Post: 07-27-2008, 01:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM