헤르메스 LIFE

[Javascript] Array Sort 본문

JSP&JavaScript&HTML

[Javascript] Array Sort

헤르메스의날개 2016. 6. 13. 11:32
728x90


출처 :http://programmingsummaries.tistory.com/108


배열 Sort


var array = [1, 10, 5, 11, 2];

//최대값
var max = array.reduce( function (previous, current) { 
	return previous > current ? previous:current;
});

//최소값
var min = array.reduce( function (previous, current) { 
	return previous > current ? current:previous;
});



var min = [1, 20, 11, 88, 3].slice(0).sort(function(a,b){a>b})[0];

var max = [1, 20, 11, 88, 3].slice(0).sort(function(a,b){a<b})[0];


["b","a","d","c"].slice(0).sort()[0];   //"a"

["b","a","d","c"].slice(0).sort().reverse()[0];  //"d"

["b","a","d","c"].slice(0).sort(function(a,b){return a > b;})[0];  //"a"

["b","a","d","c"].slice(0).sort(function(a,b){return a < b;})[0];  //"d"



728x90