Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"use strict";
var log = function log(x) {
console.log(x);
};
/*
数値配列
*/
var ary = [10, 23, 4, 0, 77];
// 単純なソート
var a = ary.sort();
log(a); // [ 0, 10, 23, 4, 77 ]
// 数的ソート
var mySort = function(a, b) {
return a - b;
};
var b = ary.sort(mySort);
log(b); // [4, 0, 77]
/*
文字列と数値の配列
*/
var mixedAry = [9, 'hello', 61, 'ant', 0];
var c = mixedAry.sort();
log(c); // [0, 61, 9, 'ant', 'hello']
var d = mixedAry.sort(mySort);
log(d); // [0, 9, 61, 'ant', 'hello']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment