Sorting in alphabetic order

Hello,

Maybe someone help me. I try to sort names in alphabetic order and get the folowing output for example: Altius Brasil ball chiken. How can I sort names in case sensitive order: Altius ball Brasil chiken?
Thanks in advance

#include <iostream>
#include <string>
using namespace std;
void sort(string arr[], const int size);
int findmin(string arr[],int start,int finish);
void swap ( string& first, string& second);
void print (string arr[], const int size);

int main()
{

const int size=10;
string names [size];

cout<< " Input name:" <<endl;
for (int i=0;i<size;i++)
{

cin>>names[i];
}
sort(names,size);
print(names,size);
system ("pause");

return 0;
}

void sort(string arr [], const int size)
{
int min=0;
for (int i=0;i<size;i++)
{
min=findmin (arr, i, size-1);
swap(arr[i],arr[min]);

}
}

int findmin (string arr [],int start,int finish)
{
int min=start;
for (int i=start+1;i<=finish;i++)
{
if (arr[i]<arr[min])
min=i;
}
return min;
}

void swap(string& first, string& second)
{
string temp=first;
first=second;
second=temp;
}
void print (string arr[], const int size)
{
for (int i=0; i<size; i++)
cout <<arr[i]<<endl;
}
Rolling your own comparison function and using that is probably the easiest way short of just converting all strings to the same case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <vector>
#include <string>
#include <cctype>

using namespace std;

bool less_than_caseless(string s1, string s2)
{
	const auto len = s1.length() > s2.length() ? s2.length() : s1.length() ;

	auto i = 0 ;

	while ( i < len && (tolower(s1[i]) == tolower(s2[i])) )
		++i ;

	if ( i == len )
		return false ;
	
	return tolower(s1[i]) < tolower(s2[i]) ;
}



int findmin (string arr [],int start,int finish)
{
	int min=start;
	for (int i=start+1;i<=finish;i++)
	{
		if ( less_than_caseless(arr[i], arr[min]) )
			min=i;
	}
	return min;
}
Topic archived. No new replies allowed.