0

How can I sort these arrays (from array with most amount of items inside to least)? I think the logic is there but there are methods I am missing. Output shows nothing.

//Declare Variables
var TN = ['Chattanooga', 'Nashville', 'Memphis'],
    FL = ['Tampa', 'Miami', 'Orlando', 'Clearwater'],
    GA = ['Atlanta', 'Marietta'];



//write function to sort array from most to least
function sortStates(a, b){
    return a - b;
}


//Make an object to get total in each array. 
var stateTotal = {
        totalTN: TN.length,
        totalFL: FL.length,
        totalGA: GA.length
};

//Sort states in order
stateTotal.sort(sortStates);

console.log(stateTotal);

Desired Output: totalFL: 4, totalTN: 3, totalGA: 2

3
  • What is your desired output? Commented Mar 14, 2020 at 17:36
  • You are doing sort directly on object. i think sort takes an array like array.sort() Commented Mar 14, 2020 at 17:36
  • @Stratubas sorry- I've included it at the bottom. I want it to sort the arrays in most to least amount of values in each one. Commented Mar 14, 2020 at 17:41

3 Answers 3

1

Stuff inside objects can't be "sorted". Stuff in arrays can be sorted.

Is this some kind of exercise? Make an object to get total in each array. is vague.

Anyway, take a look at this snippet.

//Declare Variables
var TN = ['Chattanooga', 'Nashville', 'Memphis'],
    FL = ['Tampa', 'Miami', 'Orlando', 'Clearwater'],
    GA = ['Atlanta', 'Marietta'];

//write function to sort array from most to least
function sortStates(a, b) {
    return b.total - a.total;
}

//Make an object to get total in each array. 
var stateTotal = [
    { name: 'totalTN', total: TN.length },
    { name: 'totalFL', total: FL.length },
    { name: 'totalGA', total: GA.length }
];

//Sort states in order
stateTotal.sort(sortStates);

stateTotal.forEach(info => {
  console.log(`${info.name}: ${info.total}`);
});

Sign up to request clarification or add additional context in comments.

1 Comment

perfect! I was missing the foreach part. Didn't even think about it.
0

Best way to do the sort on length of states is

var states=[];
states.push({
    name:"TN",
    towns:['Chattanooga', 'Nashville', 'Memphis']
    });
states.push({
    name:"FL",
    towns:['Tampa', 'Miami', 'Orlando', 'Clearwater']
    });
states.push({
    name:"GA",
    towns:['Atlanta', 'Marietta']
});
states.sort((state1,state2) => state2.towns.length - state1.towns.length).forEach( state => console.log(`total${state.name}:${state.towns.length}`));

Comments

0

Working with objects and arrays can be confusing. Arrays->[] may be sorted. Object properties are not sorted.

//Declare Variables
var myData = {TN:['Chattanooga', 'Nashville', 'Memphis'],
              FL:['Tampa', 'Miami', 'Orlando', 'Clearwater'],
              GA:['Atlanta', 'Marietta']};

var keys=Object.keys(myData);

var mySort=[];
for(key in keys){
   mySort.push([keys[key],myData[keys[key]].length]);
}

mySort.sort(function(a, b){
    return b[1] - a[1];
});

myObj={};

for(let i=0;i<mySort.length;i++){

myObj["total" + mySort[i][0]] = mySort[i][1]

}

console.log(myObj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.