-1

i get data from json and put in the HashMap and show in ListView Now i want sort desc data in this HashMap and show in ListVeiw

this is part of my code

for(int i=0;i<jmsg.length();i++){
    JSONObject c = jmsg.getJSONObject(i);
    String id = c.getString(TAG_ID);
    String name = c.getString(TAG_NAME);
    String email = c.getString(TAG_EMAIL);
    String subj = c.getString(TAG_SUBJ);
    String text = c.getString(TAG_TEXT);

    Log.i("Ekhteraat app ControlPanel", id);

    HashMap<String,String> nnews = new HashMap<String,String>();

    nnews.put(TAG_ID,id);
    nnews.put(TAG_NAME,name);
    nnews.put(TAG_EMAIL,email);
    nnews.put(TAG_SUBJ,subj);
    nnews.put(TAG_TEXT,text);

    msgList.add(nnews);


    nText=nText + subj + "\n";

    tnews=tnews+1;

    ListAdapter adapter = new SimpleAdapter(
                            GetMSGActivity.this, msgList, R.layout.list_item,
                            new String[]{TAG_ID,TAG_NAME,TAG_EMAIL,TAG_SUBJ,TAG_TEXT},
                            new int[]{R.id.id, R.id.name, R.id.email, R.id.subj, R.id.text}
                    );
    list.setAdapter(adapter);
}

Can you please check how can sort it.

my data like this

id,1

name,sadegh

email,sadegh@email.com

subject,test

text,hi this is test


id,2

name,rrr

email,rrr@email.com

subject,test

text,hi this is test


id,3

name,lll

email,lll@email.com

subject,test

text,hi this is test

but i want show here

id,3

name,lll

email,lll@email.com

subject,test

text,hi this is test


id,2

name,rrr

email,rrr@email.com

subject,test

text,hi this is test


id,1

name,sadegh

email,sadegh@email.com

subject,test

text,hi this is test

runDOSrun
  • 10,359
  • 7
  • 47
  • 57
sadegh
  • 1,720
  • 1
  • 16
  • 30
  • 1
    Use `TreeMap`, not `HashMap`. – Luiggi Mendoza Jul 12 '15 at 14:26
  • The keysets of HashMaps are unordered. Have a quick search on the difference of TreeMap/HashMap/LinkedHashMap as this question was asked before. – runDOSrun Jul 12 '15 at 14:29
  • @runDOSrun,in the link sort by key,they have same key but i have part of data with not same key can see in post(edit post) – sadegh Jul 12 '15 at 15:16
  • 1
    I guess you should stop using `Map` as an entity, create a proper class with relevant fields instead. And by the way you're not looking for having the data sorted but ordered the way the key/value pair was inserted, so use a `LinkedHashMap`, but again, please do not use `Map`s as entities. – Luiggi Mendoza Jul 12 '15 at 15:20
  • Your edit is just confusing. You want to sort by key, yes? Then don't use a HashMap. – runDOSrun Jul 12 '15 at 15:23
  • @runDOSrun,i have a for that each time put data with different key and add listview `nnews.put(TAG_ID,id); nnews.put(TAG_NAME,name); nnews.put(TAG_EMAIL,email); nnews.put(TAG_SUBJ,subj); nnews.put(TAG_TEXT,text);` – sadegh Jul 12 '15 at 15:27
  • Maybe i put data in array then sort array – sadegh Jul 12 '15 at 15:30
  • Never *once* have you said "I want to sort by name". That simple sentence is everything you needed. Next time, *take your time and try* formulating clear & structured sentences so people understand you. Put the names in an array + sort. Btw, Luiggi is right, you need a custom **class** for your json object not a map. – runDOSrun Jul 12 '15 at 15:40

1 Answers1

2

HashMaps are unsorted by definition. See the Javadoc:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

If you need ordering, use one of Java's SortedMaps:

A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time.

java.util has two standard implementations - TreeMap and ConcurrentSkipListMap.

Malt
  • 28,965
  • 9
  • 65
  • 105