[PATCH] locale aware sorting of stringlists

Tobias Koenig tokoe at kde.org
Mon Oct 6 22:49:10 BST 2003


Hi,

the attached patch adds locale aware sorting of QStringLists to
KStringHandler. I think there are a lot of programs which could
use this code. Can I commit it?
I'm not sure if the implementation could be done a bit smarter,
at least it works :)

Ciao,
Tobias
-- 
Can a government that shoots at reporters be democratic?
Separate politics from religion and economy!
-------------- next part --------------
Index: kstringhandler.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kstringhandler.cpp,v
retrieving revision 1.26
diff -p -u -b -r1.26 kstringhandler.cpp
--- kstringhandler.cpp	6 Jul 2003 21:21:15 -0000	1.26
+++ kstringhandler.cpp	6 Oct 2003 21:46:31 -0000
@@ -650,3 +650,36 @@ QString KStringHandler::obscure( const Q
 
   return result;
 }
+
+class LocaleAwareString : public QString
+{
+  public:
+    LocaleAwareString() : QString()
+    {}
+
+    LocaleAwareString( const QString &str ) : QString( str )
+    {}
+};
+
+static bool operator<( const LocaleAwareString &s1, const LocaleAwareString &s2 )
+{
+  return ( QString::localeAwareCompare( s1, s2 ) < 0 );
+}
+
+QStringList KStringHandler::sortLocaleAware( const QStringList &list )
+{
+  QValueList<LocaleAwareString> sortedList;
+
+  QStringList::ConstIterator it;
+  for ( it = list.begin(); it != list.end(); ++it )
+    sortedList.append( LocaleAwareString( *it ) );
+
+  qHeapSort( sortedList );
+
+  QStringList retval;
+  QValueList<LocaleAwareString>::ConstIterator retIt;
+  for ( retIt = sortedList.begin(); retIt != sortedList.end(); ++retIt )
+    retval.append( *retIt );
+
+  return retval;
+}
Index: kstringhandler.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kstringhandler.h,v
retrieving revision 1.26
diff -p -u -b -r1.26 kstringhandler.h
--- kstringhandler.h	30 Aug 2003 07:33:49 -0000	1.26
+++ kstringhandler.h	6 Oct 2003 21:46:32 -0000
@@ -403,6 +403,15 @@ public:
     */
     static QString obscure( const QString &str );
 
+    /**
+      Sorts a list of strings concerning to the local aware comparison rules
+
+      @param list the unsorted list
+      @return the sorted list
+      @since 3.2
+     */
+    static QStringList sortLocaleAware( const QStringList &list );
+
 #ifdef KDE_NO_COMPAT
 private:
 #endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20031006/99cd95cf/attachment.sig>


More information about the kde-core-devel mailing list