Skip to content

Commit f13ade9

Browse files
committed
Sort contacts alphabetically by name
1 parent b494617 commit f13ade9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/net/cyclestreets/contacts/Contact.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.cyclestreets.contacts;
22

3+
import java.util.Comparator;
4+
35
public class Contact
46
{
57
private final String name_;
@@ -22,10 +24,33 @@ public Contact(final String name,
2224
postcode_ = postcode;
2325
} // Contact
2426

27+
public String name() { return name_; }
2528
public String address() { return address_; }
2629
public String neighbourhood() { return neighbourhood_; }
2730
public String city() { return city_; }
2831
public String postcode() { return postcode_; }
2932

3033
public String toString() { return name_; }
34+
35+
static public Comparator<Contact> comparator()
36+
{
37+
return ContactsComparator.instance();
38+
} // Comparator
39+
40+
static private class ContactsComparator implements Comparator<Contact>
41+
{
42+
static private ContactsComparator instance_;
43+
static public ContactsComparator instance()
44+
{
45+
if(instance_ == null)
46+
instance_ = new ContactsComparator();
47+
return instance_;
48+
} // instance
49+
50+
@Override
51+
public int compare(final Contact lhs, final Contact rhs)
52+
{
53+
return lhs.name().compareToIgnoreCase(rhs.name());
54+
} // compare
55+
} // class ContactsComparator
3156
} // class Contact

src/net/cyclestreets/contacts/ContactsEclair.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.cyclestreets.contacts;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45
import java.util.List;
56

67
import android.content.Context;
@@ -69,6 +70,8 @@ static public List<Contact> fetch(final Context context)
6970
addrCur.close();
7071
} // finally
7172

73+
Collections.sort(contacts, Contact.comparator());
74+
7275
return contacts;
7376
} // queryContacts
7477

0 commit comments

Comments
 (0)