1

I have an array with some various information in it regarding state traits and need to modify it in a few ways. First I need to sort it by area then by name if the area is the same. So I use parseInt to get the actual int instead of the string but now I'm lost. How do I pull off this sort? I also need to find the most populated / least populated states and store that in variables.

(function() {
'use strict';

var states = [{
    "name": "Alaska",
    "area": "663,268 sq mi",
    "population": 735132
}, {
    "name": "California",
    "area": "163,696 sq mi",
    "population": 38332521
}, {
    "name": "oregon",
    "area": "98,381 sq mi",
    "population": 3899353
}, {
    "name": "washington",
    "area": "71,300 sq mi",
    "population": 6971406
}];

var moddedStates = {};
for (var i = 0; i < states.length; i++) {
    var area = parseInt(states[i].area);

}
}());
Bob
  • 13
  • 2
  • [Array.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Arun P Johny Oct 24 '14 at 03:30
  • Write your own compare function, as explained [here](http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript). Note that parseInt alone is insufficient to get the area of a state, as it will stop at the first comma. – dug Oct 24 '14 at 03:34

1 Answers1

0

I believe this snippet does everything you wanted. Note the use of replace to account for commas, and the use of parseFloat instead of parseInt, just in case some sizes include digits to the right of the decimal. (Probably not an issue when comparing sizes of U.S. states, but a good habit to get into nonetheless...)

(function() {
    'use strict';
    
    var states = [{
        "name": "Alaska",
        "area": "663,268 sq mi",
        "population": 735132
    }, {
        "name": "California",
        "area": "163,696 sq mi",
        "population": 38332521
    }, {
        "name": "oregon",
        "area": "98,381 sq mi",
        "population": 3899353
    }, {
        "name": "washington",
        "area": "71,300 sq mi",
        "population": 6971406
    }];
    
    var mostPopulated = null;
    var leastPopulated = null;

    states.sort(function (a, b) {
        var aArea = parseFloat(a.area.replace(/,/g, ''));
        var bArea = parseFloat(b.area.replace(/,/g, ''));
        if (aArea < bArea) return -1;
        else if (bArea < aArea) return 1;
        else if (a.name < b.name) return -1;
        else if (b.name < a.name) return 1;
        else return 0;
    });
    
    for (var i = 0, len = states.length; i < len; i++) {
        if (!mostPopulated || states[i].population > mostPopulated.population) {
            mostPopulated = states[i];
        }
        if (!leastPopulated || states[i].population < leastPopulated.population) {
            leastPopulated = states[i];
        }
    }
    
    console.log(states);
    console.log(mostPopulated);
    console.log(leastPopulated);
}());
Troy Gizzi
  • 2,480
  • 1
  • 14
  • 15
  • thanks! This helps a lot. I'm going to research the return values of the compare function to understand what's going on better. – Bob Oct 24 '14 at 04:39
  • 2
    No problem. I usually find MDN's documentation to be very helpful for JavaScript, HTML, and CSS. Example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Troy Gizzi Oct 24 '14 at 04:41