简体   繁体   中英

sorting arrays in descending and ascending order

write a function named compareArrays() which takes two arrays (the length of each array can be any number) and writes the first elements of this two arrays to the page in ascending order. It also writes the last elements of these two arrays in descending order. what can i correct in my code? where is the problem? The first 4 elements should be ascending and the last 4 should be descending.

 function compareArrays(y, z) { for (x = 0; x < y.length; y++) { Array.sort((Function(y, z) { return z - y }); } for (p = 0; p < z.length; z++) { Array.sort((function(y, z) { return y - z }) } } var one = [10, 20, 30, 40, 50, 60, 70, 80]; var two = [60, 70, 80, 90, 100, 110, 120, 130]; 

The problem of just displaying the original array halfway in ascending order and halfway in descending order is, that the output will be 80, 70 ,60 50, 10, 20, 30, 40,, because when the array will get the message to begin sorting in descending order it will put all numbers from 50 and up before 10 20 30 40 because its higher and it needs to go first.

So to get an output of 10, 20, 30, 40, 80, 70, 60, 50, you can split the array into 2 new arrays, then sort them, then combine them together again like this:

function sortArrays(a) {
 //slices the value of the passed in array in half, and makes 2 new arrays of it
  var arrayLength1 = a.length;
  var arrayHalf = arrayLength1 / 2;
  var firstHalf = a.slice(0,arrayHalf);
  var secondHalf = a.slice(arrayHalf);

  // sorts one array ascending and the other descending
   firstHalf.sort(ascendingFunction);
   secondHalf.sort(descendingFunction);

 //combines together the 2 arrays, already sorted one ascending and one descending
   var newSortedArray = firstHalf.concat(secondHalf); 
   alert(newSortedArray); // the output you want
    }
function ascendingFunction(a,b){
    return  a - b
    } 

function descendingFunction(a,b){
    return  b - a
    } 

var one = [10, 20, 30, 40, 50, 60, 70, 80];

var two = [60, 70, 80, 90, 100, 110, 120, 130];

now you can call the function and pass in whatever array you want, like:

sortArrays(one);
sortArrays(two);

You have quite a few syntax errors - you're also trying to sort on the Array itself, which you shouldn't do. You need to sort the arrays you put in in both ascending and descending order, and write those out:

 function compareArrays(arr1, arr2) { arr1.sort((a, b) => b - a); arr2.sort((a, b) => b - a); console.log(`First in ascending order: ${arr1[0]} and ${arr2[0]}`); arr1.sort((a, b) => a - b); arr2.sort((a, b) => a - b); console.log(`Last in descending order: ${arr1[arr1.length - 1]} and ${arr2[arr2.length - 1]}`); } var one = [10, 20, 30, 40, 50, 60, 70, 80]; var two = [60, 70, 80, 90, 100, 110, 120, 130, 140, 150]; compareArrays(one, two); 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM