Javascript/sort

解説 並び替え
sample1.2
5,3,9,1,10
sample1.2code
var a = [5,3,9,1,10];

function sample1Func(){
  a.sort(function(a,b){
    if( a < b ) return -1;
    if( a > b ) return 1;
    return 0;
  });
  var sample1 = document.getElementById("sample1");
  sample1.innerHTML = a;
}

function sample2Func(){
  a.sort(function(a,b){
    if( a > b ) return -1;
    if( a < b ) return 1;
    return 0;
  });
  var sample1 = document.getElementById("sample1");
  sample1.innerHTML = a;
}
	
sample3
sample3code
function s3Func(){
  let s3Array = [8, 4, 6, 1, 3];
  s3Array.sort();
  document.getElementById("s3Btn").after(s3Array);
}