1

How to sort HashMap<String,ArrrayList<String>>?

I am fetching contact name from phone contacts like:

"String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));"

And stored it into one HashMap<String,ArrayList<String>>. But all contacts I am getting in unsorted order.

May I know how to sort those contacts in alphabetical order so that I can display list view in sorted order?

6
  • did you search this topic before posting the question? Commented Jan 22, 2015 at 6:23
  • i searched but i got only how to parse HashMap<string,string> or HashMap<string,int> Commented Jan 22, 2015 at 6:27
  • since two days i am stucking on this Commented Jan 22, 2015 at 6:27
  • HashMap<String,ArrayList>. ArrayList of what? More strings? Commented Jan 22, 2015 at 6:28
  • Just use Comparator for that.... Commented Jan 22, 2015 at 6:34

3 Answers 3

1

You can retrieve your data from the database already sorted. Just use

String orderBy =  ContactsContract.Contacts.DISPLAY_NAME + " DESC";

in your query. After that, usually when you need to use HashMap an have also sorted data, then you use HashMap + ArrayList. In HashMap you keep normal key,value pairs, in ArrayList you keep sorted values (in case if you have already sorted data, you add it to ArrayList while reading, you don't need to sort again).

If you need to sort ArrayList which is value in your HashMap, you can use:

Collections.sort(yourArrayList);
Sign up to request clarification or add additional context in comments.

Comments

1

Use below class and pass List of data to the it. It will give you new sorted list.

public class CompareApp implements Comparator<AppDetails> {

private List<AppDetails> apps = new ArrayList<AppDetails>();

Context context;

public CompareApp(String str,List<AppDetails> apps, Context context) {
    this.apps = apps;
    this.context = context;

    Collections.sort(this.apps, this);
}

@Override
public int compare(AppDetails lhs, AppDetails rhs) {
    // TODO Auto-generated method stub
    return lhs.label.toUpperCase().compareTo(rhs.label.toUpperCase());
}

}

2 Comments

AppDetails is a generic class
public class AppDetails { public String label; public String packageName; public Drawable icon; }
0

I used following code to get Contacts in Ascending Order of String names as follows:

public void getAllContacts(ContentResolver cr) {


        Cursor phones = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, Phone.DISPLAY_NAME + " ASC");

        while (phones.moveToNext()) {
            String name = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            name1.add(name);
            phno1.add(phoneNumber);
        } 


        phones.close();
    } 

Code for reference

Hope this is what you need.

7 Comments

one more question .can you tell me how to add search option in my custom adapter for listview
@User_B You can add a EditText component as HeaderView in ListView of yours.
do you have some snippet
Just put Edit Text above listview component else
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.