json - Collection of values in android -
i have json object in format
{ "result": { "name": "ram", "mark": "50" }, { "name": "ram", "mark": "60" }, { "name": "ram", "mark": "50" }, { "name": "ram", "mark": "50" }, { "name": "ram", "mark": "50" }, { "name": "ram", "mark": "80" }, { "name": "ram", "mark": "50" } } how sort result lists depending on mark.
please help..thanks in advance.
to on own, without of helper classes, libraries, etc. can this
say have result of type object parsed out object called parsedobjects array containing results
list<list<objects>> sortedlists = new arraylist<>(); // loop through array of parsed objects for(object parsedobject : parsedobjects){ boolean match = false; // flag let know if found match // loop through our sorted lists, checking first item // in list (since items in list should have same // mark) see if mark of current parsed object matches // of sorted object's mark for(list<objects> sortedlist : sortedlists){ if(sortedlist.get(0).mark.equals(parsedobject.mark)){ sortedlist.add(parsedobject); match = true; break; } } // if didn't find match in loop, create new list // , add our list of object lists if(!match){ list<object> newlist = new arraylist<>(); newlist.add(parsedobject); sortedlists.add(newlist); } } // sortedlists list of object lists, sorted // objects' marks this because asked how it. there number of other, , more efficient ways. maps, example, streamline this.
map<string, list<object>> markmap = new hashmap<>(); for(object parsedobject : parsedobjects){ // if map has list matching mark key // list , add our object if(markmap.get(parsedobject.mark) != null){ list<object> sortedlist = markmap.get(parsedobject.mark); sortedlist.add(parsedobject); } // else create new list, add our object list // , set key map object's mark else{ list<object> newlist = new arraylist<>(); newlist.add(parsedobject); markmap.put(parsedobject.mark, newlist); } } // markmap map contains list of objects sorted // mark. list mark string key, have // call markmap.get([mark string key]); there huge number of ways this, many of them more streamlined these (this code can cleaned up/shortened wanted explicit in going on), started in ventures.
Comments
Post a Comment