xref: /haiku/src/kits/shared/NaturalCompare.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2009, Dana Burkart
3  * Copyright 2009, Stephan Aßmus <superstippi@gmx.de>
4  * Copyright 2010, Axel Dörfler, axeld@pinc-software.de
5  * Copyright 2010, Rene Gollent (anevilyak@gmail.com)
6  * Distributed under the terms of the MIT License.
7  */
8 
9 
10 #include <NaturalCompare.h>
11 
12 #include <ctype.h>
13 #include <string.h>
14 #include <strings.h>
15 
16 #include <Collator.h>
17 #include <Locale.h>
18 
19 
20 namespace BPrivate {
21 
22 
23 // #pragma mark - Natural sorting
24 
25 
26 //! Compares two strings naturally, as opposed to lexicographically
27 int
28 NaturalCompare(const char* stringA, const char* stringB)
29 {
30 	static BCollator* collator = NULL;
31 
32 	if (collator == NULL)
33 	{
34 		collator = new BCollator();
35 		BLocale::Default()->GetCollator(collator);
36 		collator->SetStrength(B_COLLATE_SECONDARY);
37 		collator->SetNumericSorting(true);
38 	}
39 
40 	return collator->Compare(stringA, stringB);
41 }
42 
43 
44 } // namespace BPrivate
45