1

I have made an class called Person made up by other classes as Name and Adress. Objects of the Person class is stored in a vector called personList. Everything works fine but my method for sorting!

I need to be able to sort the vector alphabetically, first by name & if the names are similar, by address. My issue right now is That I cant think of anyway to make the search case-insensitive.

Would apreciate any kind of help! Ive just started Learning C++.

This is the code for my function for sorting:

 bool sortByName(const Person & lhs, const Person & rhs)
{
    if (lhs.getName() == rhs.getName())
        return lhs.getAdress() < rhs.getAdress();
    else 
        return lhs.getName() < rhs.getName();
}
bobasboll
  • 13
  • 6
  • 2
    Take a look at [`std::tolower`](http://en.cppreference.com/w/cpp/string/byte/tolower). – Yuriy Ivaskevych Mar 15 '17 at 14:23
  • 1
    Possible duplicate of [Case insensitive string comparison in C++](http://stackoverflow.com/questions/11635/case-insensitive-string-comparison-in-c) – Caleth Mar 15 '17 at 14:33

2 Answers2

1

The right way to do this is implement operator<() for Person rather than your sortByName() routine:

bool operator<(const Person& lhs, const Person& rhs)
{
   ...
}

From there you can use a variety of techniques to compare your two Person objects, as long as the ordering is always the same. See Case insensitive string comparison in C++ for one solution.

Converting the case of arbitrary strings is surprisingly complex (maybe even border-line impossible), if you want to work properly for all situations, such as any language. You can side-step some of this by doing a case-insensitive comparison, rather than a conversion; but there are still a lot of issues. With additional constraints like "English only" or "just ASCII characters" this is significantly easier.

Community
  • 1
  • 1
Ðаn
  • 10,934
  • 11
  • 59
  • 95
  • Converting the case of arbitrary strings in any language is not only complex but **impossible**, because "any language" includes those which do have Latin characters or even the infamous German 'ß' which exists only in lowercase. It turns out that `std::basic_string` itself is entirely unfit for really international human-language text. – Christian Hackl Mar 15 '17 at 15:47
  • 1
    That so-called "Council for German Orthography" has been known for many stupid decisions. Inventing and standardising an upper-case 'ß' which nobody uses and which I've never seen in any printed newspaper or book in my whole life must be one of them. I wish such councils would be a bit more humble and refrain from inventing new stuff; their job should be to research and describe existing practice. – Christian Hackl Mar 15 '17 at 16:03
  • Yes, that's correct. "Straße" does become "STRASSE", but the reverse does not always work (e.g. "LASSEN" becomes "lassen"). It even depends on which variant of German we are talking about, because Swiss German does not have the 'ß', so they write "Strasse". – Christian Hackl Mar 15 '17 at 17:27
-1

Your getName() function could return the lower case of the name instead of what is actually stored (or make an alternative getName() used only for the sorting part. The tolower function (http://www.cplusplus.com/reference/cctype/tolower/) works on only one character at a time, so you would have to iterate through the whole name. But there's a solution for that, too (How to convert std::string to lower case?)

#include <algorithm>
#include <string> 

std::string data = "Abc"; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
Community
  • 1
  • 1
jalani
  • 53
  • 1
  • 7