2 This file is part of KAddressBook.
4 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "contactsorter.h"
23 #include <KContacts/Addressee>
25 class ContactSortHelper
28 ContactSortHelper(ContactFields::Field field
, Qt::SortOrder sortOrder
)
29 : mSortField(field
), mSortOrder(sortOrder
)
33 inline bool operator()(const KContacts::Addressee
&contact
,
34 const KContacts::Addressee
&otherContact
) const
37 QString::localeAwareCompare(
38 ContactFields::value(mSortField
, contact
),
39 ContactFields::value(mSortField
, otherContact
));
43 QString::localeAwareCompare(
44 ContactFields::value(ContactFields::GivenName
, contact
),
45 ContactFields::value(ContactFields::GivenName
, otherContact
));
47 if (givenNameResult
== 0) {
48 int familyNameResult
=
49 QString::localeAwareCompare(
50 ContactFields::value(ContactFields::FamilyName
, contact
),
51 ContactFields::value(ContactFields::FamilyName
, otherContact
));
53 if (familyNameResult
== 0) {
55 QString::localeAwareCompare(
56 ContactFields::value(ContactFields::FormattedName
, contact
),
57 ContactFields::value(ContactFields::FormattedName
, otherContact
));
59 result
= familyNameResult
;
62 result
= givenNameResult
;
66 bool lesser
= result
< 0;
68 if (mSortOrder
== Qt::DescendingOrder
) {
76 const ContactFields::Field mSortField
;
77 const Qt::SortOrder mSortOrder
;
80 ContactSorter::ContactSorter(ContactFields::Field field
, Qt::SortOrder sortOrder
)
81 : mSortField(field
), mSortOrder(sortOrder
)
85 void ContactSorter::sort(KContacts::Addressee::List
&contacts
) const
87 qStableSort(contacts
.begin(), contacts
.end(), ContactSortHelper(mSortField
, mSortOrder
));