<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
//1.数组升序排序
var arr1 = [6, 11, 22, 3, 1, 55];
function fnSortAsc(array, fn) {
var temp;
for (var i = 0; i < array.length - 1; i++) {
for (var j = array.length - 1; j > i; j--) {
if (fn(array[j] , array[i]) < 0) {
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
function fnCompareTwoValues(obj1, obj2) {
return obj1 - obj2;
}
fnSortAsc(arr1 , fnCompareTwoValues);
console.log('升序排序数组:' + arr1);
var arr2 = ['Kobe' , 'James' , 'Jordan' , 'Yao', 'Panshengzhi'];
fnSortAsc(arr2, function (x, y) {
return x.length - y.length;
});
console.log('升序排序字符串数组:' + arr2);
</script>
</head>
<body>
</body>
</html>