简体   繁体   中英

I can't sort numbers in an array (tried everything that was written here, stil doesnt work)

I've been doing a little game in JS and I got to the point, when I have an array with 4 numbers (one of them is the correct result, the other 3 are random). I need to sort them (ascending) but nothing seems to work. I tried E6 and E2015 funcitons that are mentioned here but nothing. I¨M including my code plus code from the console so you would see. Do you know how to deal with it or where is the problem? Thank you for your time.

//this is the code i have:
   var rnum1 = generateRandomNumber1 ();
            var rnum2 = generateRandomNumber2 ();
            var rnums = [rnum1, rnum2];
            var rnumsSort = rnums.sort(function(a, b){return b-a});
             var data= generateRandomOperatorAndCorrectResult(rnumsSort[0],rnumsSort[1]);
             //data=["+", [5]]
             var operator=data[0];
             var corResult = data[1][0][0];
             var ranResult =[data[1][0][1],data[1][0][2],data[1][0][3]];
             var allResults = data[1];
             var sortAllResults = allResults.sort(function(a,b){
    return a - b});
             var mes=alert(sortAllResults); 

//I'm sure all of my function are working because it alerts the actual 4 numbers in the array (var allResults = data[1]) but it doesnt do the sort method

 function generateRandomOperatorAndCorrectResult (num1, num2) {
        var operators = [{
            sign: "+",
            method: function(rnum1,rnum2){ return rnum1 + rnum2; } },
            {
             sign: "*",
             method: function(rnum1,rnum2){ return rnum1 * rnum2; } },
             {
             sign: "-",
            method: function(rnum1,rnum2){ return rnum1 - rnum2; }
              }];
        var results = [];

        var selectedOperator = Math.floor(Math.random()*operators.length);
        var randomOperator = operators[selectedOperator].sign;
        var correctResult = (operators[selectedOperator].method(num1, num2)); //pass the numbers to the methods
        results.push(correctResult);
        var randomResult = generateRandomResults(3);
        var result = results.concat(randomResult);
        return [randomOperator, [result]];

           }

The problem is you're trying to sort an array of arrays. When you declare other variables, you're going like this:

data[1][0][0]

This means that data is an array, containing at least one array, which contains at least one array, which contains your data.

However, when you declare allResults (the array you want to sort), you set it one level up - you're setting it equal to an array containing an array, which contains your data. So you're actually sorting an array of arrays.

To fix this, simply declare allResults like so:

var allResults = data[1][0];

And then you will be comparing numbers, not arrays.

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