0

For the given JSON:

{
    "month": {
        "days": {
            "day1": {
                "start": 13914323164815,
                "duration": 15
            },
            "day2": {
                "start": 13914123164815,
                "duration": 56
            }
        }
    }
}

I want to get the values for duration (in this case 15 and 56), compare them and return the smaller value. First i got the month object and this is what i tried next:

JSONObject days = myMonthObject.getJSONObject("days");
JSONArray daysArray = days.names();
    for (short r = 0; r<daysArray.length();r++){
             String dayObject = daysArray.getString(r);
             JSONObject allDaysObject = days.getJSONObject(dayObject); 
             String duration = allDaysObject.getString("duration"); 
             Log.w(TAG, "THE DURATION IS: " + duration);
}

On the first iteration I got the message: THE DURATION IS: 15, and on the second iteration: THE DURATION IS: 56.

Now how to preserve the first found value for duration and on the next iteration of daysArray loop (the value will be 56) to compare both and return and smaller?

Any help will be appreciated! :)

taxo
  • 539
  • 1
  • 3
  • 21

4 Answers4

1

Firstly, outisde the loop, create an int variable called max.

int max = 0;

Essentially, you're getting the value of each duration as a String (as in the example):

String duration = allDaysObject.getString("duration");

Now, you can get the int value of this by:

int intDuration = Integer.parseInt(duration);

Next, compare intDuration to max:

if (intDuration > max) max = intDuration;

Then you can let this iterate through the entire array. At the end of the loop, max will hold the highest duration.

0

Introduce a variable

String tmpDuration;
//in the for loop
if(tmpDuration.equals("") || duration < tmpDuration){
    tmpDuration = duration;
}
suizo
  • 531
  • 8
  • 24
0

Without modifying much what you've

int min = 0;    

JSONObject days = myMonthObject.getJSONObject("days");
JSONArray daysArray = days.names();
for (int i = 0; i < daysArray.lenght(); i++){
    JSONObject day = daysArray.getJSONObject(i);
    String duration= day.getString("duration");
    if(i = 0) {
        min = Integer.valueOf(duration);
    } else {
        if (Integer.valueOf(duration) < min) {
            min = Integer.valueOf(duration);
        }
    }
}

Also if you what the Object that has the highest duration

HashMap<String,Integer> map = new HashMap<String,Integer>();
ValueComparator bvc =  new ValueComparator(map);
TreeMap<String,Integer> sorted_map = new TreeMap<String,Integer>(bcv);

JSONObject days = myMonthObject.getJSONObject("days");
JSONArray daysArray = days.names();

for (JSONObject day : daysArray){
    map.put(day.toString(), Integer.valueOf(day.getString("duration"));
}

sorted_map.putAll(map);

class ValueComparator implements Comparator<String> {

    Map<String, Integer> base;
    public ValueComparator(Map<String,Integer> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with equals.    
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}

This should return

JSONObject "day1": { "start": 13914323164815, "duration": 15

15

I´ve taken the TreeMap code from here, any retribution for this goes to user157196:

Sort a Map<Key, Value> by values (Java)

Hope it helps. :)

Community
  • 1
  • 1
reixa
  • 6,903
  • 6
  • 49
  • 68
  • Thank you for the detailed answer, but i want to return the smaller value, not the larger. How to do it? :) – taxo Feb 03 '14 at 09:48
  • I´m sorry, the code I provided does return the smallest value not 56 but 15. If you navigate throught the link I posted on my answer the output of the code it´s sorted by ascending values. Just iterate the hashmap and get the first value to obtain the smallest one. – reixa Feb 03 '14 at 10:10
  • @taxo I also corrected the short answer to fulfill the condition on min value. – reixa Feb 03 '14 at 10:16
  • @ axierjhtjz sorry, i forgot to write :( I still could not get the smaller duration, by anyway i resolved my problem! Thanks! :) – taxo Feb 05 '14 at 13:06
0

Here is my solution, hope this helps:

int minValue = 99999; 
int index = 0;
for (int f = 0; f < daysArray.length(); f++){
String dayObject = daysArray.getString(f);
JSONObject allDaysObject = days.getJSONObject(dayObject); 
String duration = allDaysObject.getString("duration");

if(Integer.valueOf(duration) < minValue){
    minValue = Integer.valueOf(duration);
    index = f;                                                      
    }
}
String dayObject = daysArray.getString(index);
    JSONObject allDaysObject = days.getJSONObject(dayObject); 
    String duration = allDaysObject.getString("duration"); 
taxo
  • 539
  • 1
  • 3
  • 21