Sorting by alphabetical order Type of Hashmap values ​​in Android

advertisements

In my application i trying to sort Name by Alphabetically order in listView .Got successfully values from Local Database .May I know what is the correct way to achieve my objective? I did't find any suitable solution.Please Help me out.

My code is below

// Declare this
ArrayList<HashMap<String, Object>> Results;
ArrayList<HashMap<String, Object>>ValuesSpeciality;
public CustomAdapterSpecialityActivity adapterCustom;

 //get speciality list from local db and show on listview
public void SpecialitiesOnList()
    {
        HashMap<String , Object> temp;
        List<TaxonomyObjects> contacts = db.getAllTaxonomy();

         for (TaxonomyObjects cn : contacts)
            {
                temp=new HashMap<String, Object>();
                temp.put("provider", "" + cn.getDisplayName());
                temp.put("tvTaxonomyCode", "" + cn.getTaxonomyCode());
                System.out.println(temp);
                ValuesSpeciality.add(temp);
            }

        //searchResults=originalValuesSpeciality initially
        Results=new ArrayList<HashMap<String,Object>>        (ValuesSpeciality);

        adapterCustom=new CustomAdapterSpecialityActivity(this, R.layout.list_item_service,searchResults);
        lv.setAdapter(adapterCustom);
        adapterCustom.notifyDataSetChanged();

        for(int i=0;i<adapterCustom.getCount();i++)
        {
            HashMap<String, Object> hm = (HashMap<String, Object>) adapterCustom.getItem(i);

            hm.put("position", i);

        }

As a result my Logcat shows this

{Code=204D00000X, provider=Neuromusculoskeletal Medicine & OMM}
 {Code=204F00000X, provider=Transplant Surgery}
 {Code=204R00000X, provider=Electrodiagnostic Medicine}
 {Code=207K00000X, provider=Allergy & Immunology}
 {Code=204E00000X, provider=Oral & Maxillofacial Surgery}

I trying to show provider Name Alphabetically order.


Use This Code ...

private static HashMap sortByValues(HashMap map) { 

       List list = new LinkedList(map.entrySet());

       // Defined Custom Comparator here
       Collections.sort(list, new Comparator() {

            public int compare(Object o1, Object o2) {

               return ((Comparable) ((Map.Entry) (o1)).getValue())
                  .compareTo(((Map.Entry) (o2)).getValue());
            }
       });

       // Here I am copying the sorted list in HashMap
       // using LinkedHashMap to preserve the insertion order

     HashMap sortedHashMap = new LinkedHashMap();

       for (Iterator it = list.iterator(); it.hasNext();) {

              Map.Entry entry = (Map.Entry) it.next();

              sortedHashMap.put(entry.getKey(), entry.getValue());
       }
       return sortedHashMap;
  }