JAVA数字和字母混合排序

2024-11-29 16:24:39
推荐回答(3个)
回答1:

SELECT *

  FROM (SELECT TO_CHAR(LEVEL) BUS_NO

          FROM DUAL

        CONNECT BY LEVEL <= 10

        UNION ALL

        SELECT 'K' || LEVEL FROM DUAL CONNECT BY LEVEL <= 10)

 ORDER BY (CASE

            WHEN INSTR(BUS_NO, 'K') <> 0 THEN

             TO_NUMBER(SUBSTR(BUS_NO, 2, LENGTH(BUS_NO) - 1))

            ELSE

             TO_NUMBER(BUS_NO)

          END)

你主要看order by 后边的东西就可以了,不需要java来处理sql直接可以处理的

回答2:

public class Test
{
/**
* 混合字串数组排序。
* @param mixedStringArray 混合字串数组。必需是:{"3","K3","4","5","K4","K5"}类似;
* @return 排序后数组
*/
public static String[] sortMixedStringArray(String[] mixedStringArray)
{
String temString = null;
for (int i = 0,aLength = mixedStringArray.length; i < aLength; i++) {
for (int j = i+1; j < aLength; j++) {
if(getNumberInString(mixedStringArray[i],"K") > getNumberInString(mixedStringArray[j],"K"))
{
temString = mixedStringArray[i];
mixedStringArray[i] = mixedStringArray[j];
mixedStringArray[j] = temString;
}
}
}
temString = null;
return mixedStringArray;
}

/**
* 截取字串的第一位,保留字串中剩下的数字
* @param str 字串
* @param firstLetter
* @return 数字
*/
public static int getNumberInString(String str,String firstLetter)
{

int _int =0;
if(str.indexOf( String.valueOf(firstLetter)) == 0)
{
_int = Integer.valueOf(str.substring(1));
}else {
_int = Integer.valueOf(str);
}
return _int;
}

public static void main(String[] args) {
String[] str = {
"3",
"5",
"12",
"88",
"K3",
"K5",
"K12",
"K88"};

System.out.println(Arrays.toString(str));
System.out.println(Arrays.toString( sortMixedStringArray(str)));
}
}

回答3:

疑问,3后面是不是肯定会出现K3