JS数组排序,有些位置不懂,帮忙注释哈。为啥从小到大排序?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS数组排序,有些位置不懂,帮忙注释哈。为啥从小到大排序?相关的知识,希望对你有一定的参考价值。
代码:<script type="text/javascript">
function sortNumber(a, b)
return a - b
var numeral = new Array()
numeral[0] = "1"
numeral[1] = "200"
numeral[2] = "888"
numeral[3] = "666"
numeral[4] = "520"
numeral[5] = "1314"
document.write(numeral.sort())
document.write("<br />")
document.write(numeral.sort(sortNumber))
</script> 显示结果:1,1314,200,520,666,888
1,200,520,666,888,1314
Java怎么让数组中元素按照牌值从小到大的顺序排列
数组中元素可能包括四种花色(黑桃、红心、梅花、方块),13种牌值(A、2、3、4、5、6、7、8、9、10、J、Q、K),以及大小王,元素以类似(黑桃A、红心3、大王)的形式存在,有可能有些牌没有,也可能有些牌重复(因为不只一副牌),如何让元素按照(3、4、5、6、7、8、9、10、J、Q、K、A、2、小王、大王)的顺序排列,而且花色牌值都相同的排在一起?
你说的这些牌是用数组来存放的,所以有些麻烦。直接复制运行,更改牌的数量测试,有不懂的再问我
package test;import java.util.Arrays;
import java.util.Random;
public class DaoTest
public static void main(String[] args)
String[] str = getArr(100);
str = sorts(str);
System.out.println(Arrays.toString(str));
/**
* 从牌堆里抽出n张牌,可以有重复
*
* @param n
* @return
*/
public static String[] getArr(int n)
String[] suit = "黑桃", "红心", "梅花", "方块" ;
String[] number = "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" ;
String[] result = new String[54];
int index = 0;
for (int i = 0; i < suit.length; i++)
for (int j = 0; j < number.length; j++)
result[index] = "" + suit[i] + number[j];
index++;
result[52] = "小王";
result[53] = "大王";
String[] some = new String[n];
for (int i = 0; i < n; i++)
some[i] = result[(new Random()).nextInt(54)];
return some;
/**
* 将所有牌分为5组,排序后在放到一起
* @param arr
* @return
*/
public static String[] sorts(String[] arr)
System.out.println(Arrays.toString(arr));
StringBuffer ht = new StringBuffer("");
StringBuffer hx = new StringBuffer("");
StringBuffer mh = new StringBuffer("");
StringBuffer fk = new StringBuffer("");
StringBuffer king = new StringBuffer();
for (int i = 0; i < arr.length; i++)
if (arr[i].length() == 2)
if (king.toString().isEmpty())
king.append(arr[i]);
else
king.append("," + arr[i]);
else if (arr[i].contains("黑桃"))
if (ht.toString().isEmpty())
ht.append(arr[i]);
else
ht.append("," + arr[i]);
else if (arr[i].contains("红心"))
if (hx.toString().isEmpty())
hx.append(arr[i]);
else
hx.append("," + arr[i]);
else if (arr[i].contains("梅花"))
if (mh.toString().isEmpty())
mh.append(arr[i]);
else
mh.append("," + arr[i]);
else
if (fk.toString().isEmpty())
fk.append(arr[i]);
else
fk.append("," + arr[i]);
String[] aht = ht.toString().trim().split(",");
String[] ahx = hx.toString().trim().split(",");
String[] amh = mh.toString().trim().split(",");
String[] afk = fk.toString().trim().split(",");
String[] aking = king.toString().trim().split(",");
aht = sort1(aht);
ahx = sort1(ahx);
amh = sort1(amh);
afk = sort1(afk);
aking = sort1(aking);
String[] result = new String[arr.length];
int index = 0;
if (aht[0].length() != 0)
for (int i = 0; i < aht.length; i++)
result[index] = aht[i];
index++;
if (ahx[0].length() != 0)
for (int i = 0; i < ahx.length; i++)
result[index] = ahx[i];
index++;
if (amh[0].length() != 0)
for (int i = 0; i < amh.length; i++)
result[index] = amh[i];
index++;
if (afk[0].length() != 0)
for (int i = 0; i < afk.length; i++)
result[index] = afk[i];
index++;
if (aking[0].length() != 0)
for (int i = 0; i < aking.length; i++)
result[index] = aking[i];
index++;
return result;
/**
* 按照3-J-A-2-小王-大王的顺序排序
* @param some
* @return
*/
public static String[] sort1(String[] some)
if (some.length == 1 && some[0].length() == 0)
return some;
if (some[0].length() == 2)
Arrays.sort(some);
String[] result = new String[some.length];
for(int i = 0;i<some.length;i++)
result[i] = some[some.length-1-i];
return result;
String[] result = new String[some.length];
int[] number = new int[some.length];
String suit = "" + some[0].charAt(0) + some[0].charAt(1);
for (int i = 0; i < some.length; i++)
String s = "" + some[i].substring(2);
if ("A".equals(s))
s = "14";
else if ("J".equals(s))
s = "11";
else if ("Q".equals(s))
s = "12";
else if ("K".equals(s))
s = "13";
else if ("2".equals(s))
s = "15";
number[i] = Integer.parseInt(s);
Arrays.sort(number);
for (int i = 0; i < result.length; i++)
String s = "" + number[i];
if (number[i] == 11)
s = "J";
else if (number[i] == 12)
s = "Q";
else if (number[i] == 13)
s = "K";
else if (number[i] == 14)
s = "A";
else if (number[i] == 15)
s = "2";
result[i] = suit + s;
return result;
参考技术A 运行结果是:
牌值a
花色红桃
牌值2
花色方块
牌值3
花色黑桃
牌值4
花色梅花
牌值5
花色红桃
牌值6
花色方块
牌值7
花色黑桃
牌值8
花色梅花
牌值9
花色红桃
牌值10
花色方块
牌值j
花色黑桃
牌值q
花色梅花
牌值k
花色红桃
牌值a
花色方块
牌值2
花色黑桃
牌值3
花色梅花
牌值4
花色红桃
牌值5
花色方块
牌值6
花色黑桃
牌值7
花色梅花
牌值8
花色红桃
牌值9
花色方块
牌值10
花色黑桃
牌值j
花色梅花
牌值q
花色红桃
牌值k
花色方块
牌值a
花色黑桃
牌值2
花色梅花
牌值3
花色红桃
牌值4
花色方块
牌值5
花色黑桃
牌值6
花色梅花
牌值7
花色红桃
牌值8
花色方块
牌值9
花色黑桃
牌值10
花色梅花
牌值j
花色红桃
牌值q
花色方块
牌值k
花色黑桃
牌值a
花色梅花
牌值2
花色红桃
牌值3
花色方块
牌值4
花色黑桃
牌值5
花色梅花
牌值6
花色红桃
牌值7
花色方块
牌值8
花色黑桃
牌值9
花色梅花
牌值10
花色红桃
牌值j
花色方块
牌值q
花色黑桃
牌值k
花色梅花
以上是关于JS数组排序,有些位置不懂,帮忙注释哈。为啥从小到大排序?的主要内容,如果未能解决你的问题,请参考以下文章