Quicksort with strings and integers

Jun 21, 2009 at 8:56pm
I need to use quicksort to sort data by street name first then house number for my C++ course. I understand how to use quicksort and also how to sort strings, but how do I sort by string first then by integer second, assuming there are multiple house numbers on one street. Help!
Jun 21, 2009 at 9:10pm
for example the list

123 Jones
376 Adams
475 Jones
321 Adams

would sort to

321 Adams
376 Adams
123 Jones
475 Jones

Am using a class that includes the address and an array of pointers of the class type
Jun 22, 2009 at 8:27am
Figured it out. Sort by street name first, then another sort function that will compare numbers if the street names are the same
Jun 22, 2009 at 12:46pm
You only need one sort; you just need a comparator function that breaks ties on names by looking at the street number next.

1
2
3
4
5
6
7
8
9
10
11
struct Info {
    std::string name;
    int           addr;
};

bool operator<( const Info& lhs, const Info& rhs ) {
    // return boost::tie( lhs.name, lhs.addr ) < boost::tie( rhs.name, rhs.addr );
    if( lhs.name < rhs.name ) return true;
    if( rhs.name < lhs.name ) return false;
    return lhs.addr < rhs.addr;
}
Topic archived. No new replies allowed.