简体   繁体   English

使用javascript中的sort方法对数组进行排序

[英]Sort an array with the sort method in javascript

I have written this code but it isn't working . 我写了这段代码,但它没有用。 It's displaying the unsorted array as well as the button but when I click on the button nothing happens. 它显示未排序的数组以及按钮,但是当我点击按钮时没有任何反应。 I am new to javascript. 我是javascript的新手。 What I know so far is that we can call functions using the onclick method by javascript. 到目前为止我所知道的是我们可以通过javascript使用onclick方法调用函数。 We can write functions as we write in c or c++ . 我们可以用c或c ++编写函数。 That's what I think I have done here but it isn't showing the sorted array . 这就是我认为我在这里所做的,但它没有显示已排序的数组。

 var myarray = [4, 6, 2, 1, 9, ]; document.getElementById("demo").innerHTML = myarray; function sort(myarray) { var count = array.length - 1, swap, j, i; for (j = 0; j < count; j++) { for (i = 0; i < count; i++) { if (array[i] > myarray[i + 1]) { swap = myarray[i + 1]; myarray[i + 1] = myarray[i]; myarray[i] = swap; } } document.write(myarray); } } 
 <p>Click the button to sort the array.</p> <button onclick="sort()">Try it</button> <p id="demo"></p> 

The main problem is, to decide if a parameter should be set or not. 主要问题是,决定是否应该设置参数。 If not, then use myarray for all operations. 如果没有,那么使用myarray进行所有操作。 If you decide to use a parameter, then use only the parameter variable. 如果您决定使用参数,则仅使用参数变量。

 var myarray = [4, 6, 2, 1, 9, ]; document.getElementById("demo").innerHTML = myarray; function sort() { // no need for a parameter var count = myarray.length - 1, // change it to myarray swap, j, i; for (j = 0; j < count; j++) { for (i = 0; i < count; i++) { if (myarray[i] > myarray[i + 1]) { // access only myarray swap = myarray[i + 1]; myarray[i + 1] = myarray[i]; myarray[i] = swap; } } } document.getElementById("demo").innerHTML = myarray; // return the sorted array } 
 <p>Click the button to sort the array.</p> <button onclick="sort()">Try it</button> <p id="demo"></p> 

 var myarray = [4, 6, 2, 1, 9]; document.getElementById("demo").innerHTML = myarray; function sort(myarray) { var count = myarray.length - 1, swap, j, i; for (j = 0; j < count; j++) { for (i = 0; i < count; i++) { if (myarray[i] > myarray[i + 1]) { swap = myarray[i + 1]; myarray[i + 1] = myarray[i]; myarray[i] = swap; } } } document.getElementById("result").innerHTML = myarray; } 
 <p>Click the button to sort the array.</p> <button onclick="sort(myarray)">Try it</button> <p id="demo"></p> <p id="result"></p> 

I found 2 problems with your code. 我发现你的代码有2个问题。 The first is that you wrote array instead of myarray in 2 lines: 第一个是你用2行写了array而不是myarray

var count = array.length - 1,

and

if (array[i] > myarray[i + 1]) {

The second problem is that you didn't pass your array as argument, when calling the function, so: 第二个问题是,在调用函数时,您没有将数组作为参数传递,因此:

<button onclick="sort()">Try it</button>

should be: 应该:

<button onclick="sort(myarray)">Try it</button>

Firstly you array definition has an extra , at the end. 首先你数组定义有一个额外的,在最后。 Next, you have made use of array and not myarray in the if statement. 接下来,您在if语句中使用了array而不是myarray

Once you sort, you need to have a statement: 排序后,您需要声明:

document.getElementById("demo").innerHTML = myarray;

You are using a variable 'array' which is not defined or known to the function. 您正在使用函数未定义或未知的变量“数组”。 Moreover on buttonclick you are not passing any parameter to sort function. 此外,在buttonclick上,您没有将任何参数传递给sort函数。 That's the reason you're getting an error. 这就是你收到错误的原因。

var myarray = [4, 6, 2, 1, 9, ];

function sort(myarray) {
  var count = myarray.length - 1,
    swap,
    j,
    i;

  for (j = 0; j < count; j++) {
    for (i = 0; i < count; i++) {
      if (myarray[i] > myarray[i + 1]) {
        swap = myarray[i + 1];
        myarray[i + 1] = myarray[i];
        myarray[i] = swap;
      }
    }
    document.write(myarray);
  }
}

<p>Click the button to sort the array.</p>
<button onclick="sort(myarray)">Try it</button>
<p id="demo"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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