java - Remove elements with same date from JSONArray -
so, have sort jsonarray contains jsonobject.
strucuture of json following:
[ { "id": 582, "istransaction": false, "todate": "2015-08-26 16:12:00.0", "fromdate": "2015-08-24 15:11:00.0", "status": "request_accepted_by_both_sides" }, { "id": 602, "istransaction": false, "todate": "2015-08-21 21:52:00.0", "fromdate": "2015-08-20 23:53:00.0", "status": "request_accepted_by_both_sides" }, { "carfk": 1, "endmileage": 1455, "ownername": "celien", "model": "335i", "status": "driver_set_odometer", "ownerid": 1, "todate": "2015-08-26 16:12:00.0", "startmileage": 455, "id": 421, "fromdate": "2015-08-24 15:11:00.0", "istransaction": true, "brand": "bmw", "drivername": "damien", "exchange": false } ]
what have delete jsonobjects have same fromdate field. if 2 object have same field, keep 1 wich contains field istransaction:true
here algortithm wrote :
private void addonlyrequestortransactionintolistview(jsonarray array) { jsonarray copyarray = array; jsonarray resultarray = new jsonarray(); int = 0, j = 0; try { while(i < array.length()){ jsonobject temp = array.getjsonobject(i); j = 0; while(j < copyarray.length()){ if(temp.getstring("fromdate").equals(array.getjsonobject(j).getstring("fromdate"))) if(temp.getboolean("istransaction")) resultarray.put(temp); j++; } i++; } } catch (jsonexception e) { e.printstacktrace(); } }
but not want performed.
can give me clues? thanks!
you're comparing every element every element, including itself. should skip case i == j
so instead of:
if(temp.getstring("fromdate").equals(array.getjsonobject(j).getstring("fromdate")))
use:
if(i != j && temp.getstring("fromdate").equals(array.getjsonobject(j).getstring("fromdate")))
Comments
Post a Comment