Sort an array from bottom to top and reverse. Get only the 10% of the total array











up vote
0
down vote

favorite
1















"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96, 
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]

function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}


function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}


function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});

getTenPerOfMissed(members);
}


bottomTenPercent();
TopTenPercent();





I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb










share|improve this question
























  • Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
    – James
    Nov 11 at 14:02










  • What do you mean by 10% ? If the input is [1, 2, 3, 4, 5, 6, 7, 8, 9] whats the expected output?
    – Jonas Wilms
    Nov 11 at 14:07










  • can you share the array?
    – brk
    Nov 11 at 14:08












  • Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
    – Ioan Dimi
    Nov 11 at 14:09










  • What should the output from those numbers be. Make it clear so you get a good answer.
    – James
    Nov 11 at 14:15















up vote
0
down vote

favorite
1















"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96, 
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]

function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}


function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}


function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});

getTenPerOfMissed(members);
}


bottomTenPercent();
TopTenPercent();





I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb










share|improve this question
























  • Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
    – James
    Nov 11 at 14:02










  • What do you mean by 10% ? If the input is [1, 2, 3, 4, 5, 6, 7, 8, 9] whats the expected output?
    – Jonas Wilms
    Nov 11 at 14:07










  • can you share the array?
    – brk
    Nov 11 at 14:08












  • Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
    – Ioan Dimi
    Nov 11 at 14:09










  • What should the output from those numbers be. Make it clear so you get a good answer.
    – James
    Nov 11 at 14:15













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1








"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96, 
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]

function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}


function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}


function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});

getTenPerOfMissed(members);
}


bottomTenPercent();
TopTenPercent();





I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb










share|improve this question


















"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96, 
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]

function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}


function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}


function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});

getTenPerOfMissed(members);
}


bottomTenPercent();
TopTenPercent();





I have create these three functions to sort an array from higher to lower and reverse. Also i need to keep only the 10% percent of the numbers and i have to keep the number that they are repeated.
When I print my temp array with the sorted numbers are unable to work together both of the sorted functions. If i call only one of them work perfect.
Also i need to have access in the temp because i want to print the numbers in a table. What process i have to follow because i can't realize at all the process and when i try to set the temp as an global variable my functions doesn't work well
Any ideas what i am doing wrong? because i have stuck on this task for two days. I am new in javascript so forgive me if my the question is dumb






"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96, 
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]

function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}


function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}


function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});

getTenPerOfMissed(members);
}


bottomTenPercent();
TopTenPercent();





"missed_votes_pct" = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96, 
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42]

function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
console.log(temp);
}


function bottomTenPercent() {
members.sort(function(a, b) {
return a.missed_votes_pct - b.missed_votes_pct; //sort our array from lower to higher and call getTenPerOfMissed(members)
//to keep the 10% of the lowest values
});
// console.log("Lowest" , members)
getTenPerOfMissed(members)
}


function TopTenPercent() {
members.sort(function(a, b) {
return b.missed_votes_pct - a.missed_votes_pct;
});

getTenPerOfMissed(members);
}


bottomTenPercent();
TopTenPercent();






javascript arrays object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 14:14

























asked Nov 11 at 13:59









Ioan Dimi

235




235












  • Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
    – James
    Nov 11 at 14:02










  • What do you mean by 10% ? If the input is [1, 2, 3, 4, 5, 6, 7, 8, 9] whats the expected output?
    – Jonas Wilms
    Nov 11 at 14:07










  • can you share the array?
    – brk
    Nov 11 at 14:08












  • Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
    – Ioan Dimi
    Nov 11 at 14:09










  • What should the output from those numbers be. Make it clear so you get a good answer.
    – James
    Nov 11 at 14:15


















  • Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
    – James
    Nov 11 at 14:02










  • What do you mean by 10% ? If the input is [1, 2, 3, 4, 5, 6, 7, 8, 9] whats the expected output?
    – Jonas Wilms
    Nov 11 at 14:07










  • can you share the array?
    – brk
    Nov 11 at 14:08












  • Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
    – Ioan Dimi
    Nov 11 at 14:09










  • What should the output from those numbers be. Make it clear so you get a good answer.
    – James
    Nov 11 at 14:15
















Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02




Hi there, to properly illustrate your question can you give us examples of the input, and what you expect the output to be?
– James
Nov 11 at 14:02












What do you mean by 10% ? If the input is [1, 2, 3, 4, 5, 6, 7, 8, 9] whats the expected output?
– Jonas Wilms
Nov 11 at 14:07




What do you mean by 10% ? If the input is [1, 2, 3, 4, 5, 6, 7, 8, 9] whats the expected output?
– Jonas Wilms
Nov 11 at 14:07












can you share the array?
– brk
Nov 11 at 14:08






can you share the array?
– brk
Nov 11 at 14:08














Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09




Thank you very much. The output should be array.length * 0.1 and if there are repeated number should me included it as well.
– Ioan Dimi
Nov 11 at 14:09












What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15




What should the output from those numbers be. Make it clear so you get a good answer.
– James
Nov 11 at 14:15












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.



function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}

var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed

// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);





share|improve this answer





















  • So now you have define each array in a new variable?
    – Ioan Dimi
    Nov 11 at 14:40












  • Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
    – James
    Nov 11 at 15:03












  • Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
    – Ioan Dimi
    Nov 11 at 15:07


















up vote
0
down vote













When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct, this expect missed_votes_pct to be an key inside an object.In that case the array will look like this



[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]


Only sorting missed_votes_pct which is just an array will be fine. You can either use Math.ceil or Math.floor to get the round digit because 10% array length can also give a floating number.



You can use splice to create a sub array.



Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany) to get top 10 & array.splice(-10) to get last 10 which highest






let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];


function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);

}


function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});

return getTenPerOfMissed(memsortedMems)
}


function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}


console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));








share|improve this answer























  • Thank everyone for your support I solve it . I can't thank you enough guys!!!
    – Ioan Dimi
    Nov 11 at 15:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249480%2fsort-an-array-from-bottom-to-top-and-reverse-get-only-the-10-of-the-total-arra%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.



function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}

var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed

// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);





share|improve this answer





















  • So now you have define each array in a new variable?
    – Ioan Dimi
    Nov 11 at 14:40












  • Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
    – James
    Nov 11 at 15:03












  • Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
    – Ioan Dimi
    Nov 11 at 15:07















up vote
1
down vote



accepted










The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.



function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}

var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed

// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);





share|improve this answer





















  • So now you have define each array in a new variable?
    – Ioan Dimi
    Nov 11 at 14:40












  • Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
    – James
    Nov 11 at 15:03












  • Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
    – Ioan Dimi
    Nov 11 at 15:07













up vote
1
down vote



accepted







up vote
1
down vote



accepted






The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.



function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}

var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed

// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);





share|improve this answer












The easiest way to open up that temp array is to return it from your getTenPerOfMissed function. Then, whatever function called it has access to the modified array.



function getTenPerOfMissed(array) {
var temp = ;
var len = array.length * 0.1;
for (var i = 0; i < array.length; i++) {
// console.log(array[i].missed_votes_pct);

if (i < len) {
temp.push(array[i]);
} else if (array[i].missed_votes_pct == array[i - 1].missed_votes_pct) { //find the 10% of the lowest values and keep also values that repeated
temp.push(array[i]);
} else {
break;
}
}
return temp;
}

var topten = getTenPerOfMissed(array);
// topten is the "temp" array from getTenPerOfMissed

// sort array differently
array.sort(...);
var bottomten = getTenPerOfMissed(array);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 14:37









James

11.6k21430




11.6k21430












  • So now you have define each array in a new variable?
    – Ioan Dimi
    Nov 11 at 14:40












  • Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
    – James
    Nov 11 at 15:03












  • Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
    – Ioan Dimi
    Nov 11 at 15:07


















  • So now you have define each array in a new variable?
    – Ioan Dimi
    Nov 11 at 14:40












  • Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
    – James
    Nov 11 at 15:03












  • Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
    – Ioan Dimi
    Nov 11 at 15:07
















So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40






So now you have define each array in a new variable?
– Ioan Dimi
Nov 11 at 14:40














Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03






Yeah, and then you work with the topten and bottomten variables, writing them out to your display or whatever you're going to do with them.
– James
Nov 11 at 15:03














Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07




Really thanks, it's so easy, but my mind did not work ον this way I need a lot of practice ....
– Ioan Dimi
Nov 11 at 15:07












up vote
0
down vote













When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct, this expect missed_votes_pct to be an key inside an object.In that case the array will look like this



[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]


Only sorting missed_votes_pct which is just an array will be fine. You can either use Math.ceil or Math.floor to get the round digit because 10% array length can also give a floating number.



You can use splice to create a sub array.



Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany) to get top 10 & array.splice(-10) to get last 10 which highest






let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];


function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);

}


function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});

return getTenPerOfMissed(memsortedMems)
}


function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}


console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));








share|improve this answer























  • Thank everyone for your support I solve it . I can't thank you enough guys!!!
    – Ioan Dimi
    Nov 11 at 15:12















up vote
0
down vote













When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct, this expect missed_votes_pct to be an key inside an object.In that case the array will look like this



[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]


Only sorting missed_votes_pct which is just an array will be fine. You can either use Math.ceil or Math.floor to get the round digit because 10% array length can also give a floating number.



You can use splice to create a sub array.



Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany) to get top 10 & array.splice(-10) to get last 10 which highest






let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];


function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);

}


function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});

return getTenPerOfMissed(memsortedMems)
}


function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}


console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));








share|improve this answer























  • Thank everyone for your support I solve it . I can't thank you enough guys!!!
    – Ioan Dimi
    Nov 11 at 15:12













up vote
0
down vote










up vote
0
down vote









When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct, this expect missed_votes_pct to be an key inside an object.In that case the array will look like this



[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]


Only sorting missed_votes_pct which is just an array will be fine. You can either use Math.ceil or Math.floor to get the round digit because 10% array length can also give a floating number.



You can use splice to create a sub array.



Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany) to get top 10 & array.splice(-10) to get last 10 which highest






let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];


function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);

}


function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});

return getTenPerOfMissed(memsortedMems)
}


function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}


console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));








share|improve this answer














When you are sorting , you dont need this a.missed_votes_pct - b.missed_votes_pct, this expect missed_votes_pct to be an key inside an object.In that case the array will look like this



[{missed_votes_pct:someVal},{missed_votes_pct:someVal}]


Only sorting missed_votes_pct which is just an array will be fine. You can either use Math.ceil or Math.floor to get the round digit because 10% array length can also give a floating number.



You can use splice to create a sub array.



Technically you dont need two function to get top and bottom 10%.You can sort in ascending order & use array.splice(start,howmany) to get top 10 & array.splice(-10) to get last 10 which highest






let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];


function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);

}


function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});

return getTenPerOfMissed(memsortedMems)
}


function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}


console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));








let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];


function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);

}


function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});

return getTenPerOfMissed(memsortedMems)
}


function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}


console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));





let missed_votes_pct = [4.61, 0.53, 0.18, 0.89, 0.53, 2.3, 4.96,
0.71, 0.35, 4.26, 0.35, 1.06, 0.89, 1.06,
0.18, 0.89, 9.54, 0, 3.9, 4.26, 0.35, 1.77,
0.35, 0.53, 5.67, 0.35, 1.42, 13.65, 2.66,
0.18, 0.18, 6.38, 0.71, 8.51, 4, 0.35, 0.35,
5.14, 0, 0.35, 0.53, 0.35, 4.61, 3.01, 4.43,
2.13, 1.24, 1.7, 2.13, 10.99, 0.53, 2.09,
0.53, 0.35, 0, 0.53, 0, 0.35, 3.01, 1.77,
0.89, 0.53, 45.56, 2.48, 0, 14.89, 1.77,
4.43, 3.19, 0.35, 2.84, 6.21, 3.55, 1.24,
0.89, 0.71, 0, 0.89, 1.24, 1.6, 6.21, 2.48,
1.06, 2.13, 0.18, 0.89, 65, 3.19, 0.89, 0,
0.89, 3.4, 3.55, 1.06, 0, 3.37, 4.96, 1.06, 0.71,
1.42
];


function getTenPerOfMissed(array) {
var temp = ;
var len = Math.ceil(array.length * 0.1);
return array.splice(0, 10);

}


function bottomTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return a - b;
});

return getTenPerOfMissed(memsortedMems)
}


function TopTenPercent(members) {
let memsortedMems = members.sort(function(a, b) {
return b - a;
});
return getTenPerOfMissed(memsortedMems);
}


console.log(bottomTenPercent(missed_votes_pct));
console.log(TopTenPercent(missed_votes_pct));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 15:05

























answered Nov 11 at 14:50









brk

25.1k31939




25.1k31939












  • Thank everyone for your support I solve it . I can't thank you enough guys!!!
    – Ioan Dimi
    Nov 11 at 15:12


















  • Thank everyone for your support I solve it . I can't thank you enough guys!!!
    – Ioan Dimi
    Nov 11 at 15:12
















Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12




Thank everyone for your support I solve it . I can't thank you enough guys!!!
– Ioan Dimi
Nov 11 at 15:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249480%2fsort-an-array-from-bottom-to-top-and-reverse-get-only-the-10-of-the-total-arra%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)