“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Recently used the string sort problem, the main functions include:

1: Sort Chinese strings

2: English string sort

3: sort numbers

4: string letter conversion

Then, I will introduce these functions in detail

1: Sort Chinese strings

Chinese characters are extensive and profound, and the most rare sorting time is also the sorting of Chinese strings. However, a very convenient class is provided in Qt to solve the problem of sorting: the QLocale class

The QLocale class converts numbers to and from their string representation in a variety of languages. QLocale initializes language/country pairs in its constructor and provides numeric to string and string to number conversion functions similar to those in QString.

For the use of the above class, let’s solve how to sort the Chinese string!

  • Positive sorting of Chinese strings
QLocale loc(QLocale::Chinese, QLocale::China);
Copy the code

Construct the QLocale class, and specify the use of Chinese format conversion form

 loc.languageToString(QLocale::Chinese);
Copy the code

Returns a QString containing the language name. After retrieving the converted information, the string information is sorted in a new way

QCollator qoc(loc); qSort(listData.begin(), listData.end(), qoc); // forward sortCopy the code
  • Sort Chinese strings backwards

QSort is similar to positive sort, except for some changes in qSort. Other things are the same, so I won’t go into details here

2: English string sort

For this functionality we continue with the class of the first one: QLocale

The implementation of the writing is similar, just convert Chinese into English

QLocale loc(QLocale::English);
Copy the code

Constructs the QLocale class information of the English type

loc.languageToString(QLocale::English);
Copy the code

Returns a QString containing the language name. After retrieving the converted information, the string information is sorted in a new way

QCollator qoc(loc); qSort(listData.begin(), listData.end(), qoc); // forward sortCopy the code

3: sort numbers

We use a vector to store the data to be sorted.

For a container that stores only one value, sort is all you need to do.

It is important to note, however, that using a set requires its own overloaded comparison operator, so we would normally use a sequential Vector

Suppose that the container for storing data is defined as: STD ::vector vetData

Positive sort of integer data

sort(vetData.begin(), vetData.end());
Copy the code

Sort integer data backwards

sort(vetData.rbegin(), vetData.rend());
Copy the code

4: string letter conversion

For CString strings, an API can do that, so what if we convert them ourselves?

Although the function is more base class, as their own learning!

We know that uppercase and lowercase characters in ASCII do not agree. So, when we encounter a lowercase value, directly replace the ASCII value of uppercase, we can achieve string conversion!

for (int i=0; i< sText.length(); i++)
{
	if (sText[i] >= 'A' && sText[i] <= 'Z')
	{
		sText[i] += 32;
		continue;
	}
}
return sText;
Copy the code

Today’s function is relatively simple, update to here ~