版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/themagickeyjianan/article/details/88062493
/**
*
* @type {number[]}
*/
let cards = [
101, 102, 101, 103, 103, 104, 103, 103, 104, 104,104, 104, 102, 103, 103
];
console.log("\n排序前 =", cards);
cards.sort(function (a, b) {
return (b%100) != (a%100) ? ((a%100) - (b%100)) : (parseInt(a/100) - parseInt(b/100));
});
console.log("\n手牌按照-->数值和花色从小到大排序后 =", cards);
/**
* 牌数量排序
* @param arr
* @param num
* @returns {number}
*/
function count(arr, num) {
let c = 0;
for(let i = 0; i < arr.length; i++){
if(arr[i] == num){
c++;
}
}
return c;
}
cards.sort(function (a, b) {
var aCount = count(cards, a);
var bCount = count(cards, b);
return (aCount != bCount) ? (aCount - bCount) : (a-b);
});
console.log("\n出的3带1按照-->按照牌数量 和 数值从小到大排序后 =", cards);
/**
排序前 = [ 101, 102, 101, 103, 103, 104, 103, 103, 104, 104, 104, 104, 102, 103, 103 ]
手牌按照-->数值和花色从小到大排序后 = [ 101, 101, 102, 102, 103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104 ]
出的3带1按照-->按照牌数量 和 数值从小到大排序后 = [ 101, 101, 102, 102, 104, 104, 104, 104, 104, 103, 103, 103, 103, 103, 103 ]
*/