【问题标题】:Sorting Alphabetically Type Of Hashmap Values In Android在 Android 中按字母顺序对 Hashmap 值类型进行排序
【发布时间】:2015-04-10 03:54:44
【问题描述】:

在我的应用程序中,我尝试在 listView 中按 Alphabetally 顺序对 Name 进行排序。从 Local Database 中成功获取值。请问我知道什么是实现我的目标的正确方法?我没有找到任何合适的解决方案。请帮帮我。

我的代码在下面

// 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);

    }
}

结果我的 Logcat 显示了这个

{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}

我试图显示供应商姓名Alphabetically订单。

【问题讨论】:

  • 您不对哈希映射进行排序。哈希映射本质上是未排序的——它使用哈希表,这是一种无序数据类型。如果要对数据进行排序,请使用合适的数据结构,例如列表或树。

标签: android arraylist hashmap adapter


【解决方案1】:

使用此代码 ...

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;
  }

【讨论】:

    【解决方案2】:

    如果您使用的是本地数据库,那么您可以轻松地在 TEXT 字段上使用 order by,这将给出结果并使用 LinkedHashMap 而不是 HashMap,您的问题将很容易解决

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 2013-11-09
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 2017-02-19
      • 2013-09-09
      相关资源
      最近更新 更多