新手上路,摸着写了个英文字母+数字的混合排序
还请各位前辈多指教
数字排序按照大小排列,一个数字中出现2个小数点认作字符
一样大小的数字将按照位数多少排列
注释的日语不清楚可以问我
import studyA.A21_StrArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.lang.Long;/**
 * @文字列動的配列クラス、便利機能/ソート機能
 * @author darkankh
 */public class A22_StrArray extends A21_StrArray
{
//A21_StrArrayのオブジェクト
private static A21_StrArray a21 = new A21_StrArray();

//昇順モード
private static final int ASC_SORT = 0; //降順モード
private static final int DESC_SORT = 1; //全ての要素を配列で取得
public String[] getAll()
{
//メモリのデータ数を取得
int memoLen = a21.get_BASARAY().length;

//メモリを取得
String[] meMo = a21.get_BASARAY();

//メモリは空の場合
if( memoLen == 0 )
{
System.out.println("メモリは空の状態です、確認してください!");
}

//予想外
if( memoLen < 0 )
{
System.out.println("予想外のエラー発生~!");
}

return meMo;
}

//要素数を取得する方法
public int getIndex()
{
int index = a21.get_BASARAY().length;

return index;
} //引数mode ASC_SORT:昇順/DESC_SORT:降順
public void sort(int mode)
{
//メモリのデータ数を取得
int memoLen = a21.get_BASARAY().length;

//メモリを取得
String[] meMo = a21.get_BASARAY();

//unicodeで降順の算法
Comparator<String> cmpRev = new Comparator<String>() 

public int compare(String str1, String str2) 
{
int uniDesc = 0;

if( str2 == null )
{
uniDesc = -1;
}
else if( str1 == null )
{
uniDesc = 1;
}
else
{
uniDesc = str2.compareTo(str1);
}

return uniDesc;
}
};

//文字列の長さまたは桁数によって降順の算法
Comparator<String> lenRev = new Comparator<String>() 

public int compare(String str1, String str2) 
{
int rev = 0;

if( str2 == null )
{
rev = -1;
}
else if( str1 == null )
{
rev = 1;
}
else
{   
rev = str2.length() - str1.length();
}

return rev;
}
};

//unicodeで昇順の算法
Comparator<String> cmp = new Comparator<String>() 

public int compare(String str1, String str2) 
{
int uniAsc = 0;

if( str2 == null )
{
uniAsc = 1;
}
else if( str1 == null )
{
uniAsc = -1;
}
else
{
uniAsc = str1.compareTo(str2);
}
return uniAsc;
}
};

//文字列の長さまたは桁数によって昇順の算法
Comparator<String> len = new Comparator<String>() 

public int compare(String str1, String str2) 
{
int num = 0;

if( str2 == null )
{
num = 1;
}
else if( str1 == null )
{
num = -1;
}
else
{
num = str1.length() - str2.length();
}
return num;
}
};

//数字の数を確認
int cnt = 0;

for( int i = 0; i < memoLen; i ++ )
{
if( meMo[i].matches( "^(-?\\d+)(\\.\\d+)?$") || meMo[i].matches( "^-?\\d+$" ) )
{
cnt++;
}
}
//数字文字列昇順の算法
Comparator<String> num = new Comparator<String>() 

public int compare(String str1, String str2) 
{
double db1 = 0;
double db2 = 0;

try
{
if( str1.matches( "^(-?\\d+)(\\.\\d+)?$") )
{
db1 = Double.parseDouble( str1 );
}
if( str2.matches( "^(-?\\d+)(\\.\\d+)?$") )
{
db2 = Double.parseDouble( str2 );
}
if( str1.matches( "^-?\\d+$" ) )
{
db1 = Long.valueOf( str1 ).doubleValue();
}
if( str2.matches( "^-?\\d+$" ) )
{
db2 = Long.valueOf( str2 ).doubleValue();
}
}
catch( Exception e )
{
System.out.println("予想外のエラー発生~!");
}
int num = 0;

if( str2 == null )
{
num = 1;
}
else if( str1 == null )
{
num = -1;
}
else
{
if( db2 != db1 )
{
double temp = db1 - db2;
if( temp > 0 )
{
num = 1;
}
if( temp < 0 )
{
num = -1;
}
}
else
{
num = str1.length() - str2.length();
}
}
return num;
}
}; //数字文字列降順の算法
Comparator<String> numRev = new Comparator<String>() 

public int compare(String str1, String str2) 
{
double db1 = 0;
double db2 = 0;

try
{
if( str1.matches( "^(-?\\d+)(\\.\\d+)?$") )
{
db1 = Double.parseDouble( str1 );
}
if( str2.matches( "^(-?\\d+)(\\.\\d+)?$") )
{
db2 = Double.parseDouble( str2 );
}
if( str1.matches( "^-?\\d+$" ) )
{
db1 = Long.valueOf( str1 ).doubleValue();
}
if( str2.matches( "^-?\\d+$" ) )
{
db2 = Long.valueOf( str2 ).doubleValue();
}
 
}
catch( Exception e )
{
System.out.println("予想外のエラー発生~!");
}
int num = 0;

if( str2 == null )
{
num = -1;
}
else if( str1 == null )
{
num = 1;
}
else
{
if( db2 != db1 )
{
double temp = db2 - db1;
if( temp > 0 )
{
num = 1;
}
if( temp < 0 )
{
num = -1;
}
}
else
{
num = str2.length() - str1.length();
}
}
return num;
}
};

//数字部分の臨時配列
String[] tempNum = new String[cnt]; //文字列部分の臨時配列
String[] tempStr = new String[memoLen-cnt]; int numCnt = 0;
int strCnt = 0; for( int i = 0; i < memoLen; i ++ )
{
if( meMo[i].matches( "^(-?\\d+)(\\.\\d+)?$") || meMo[i].matches( "^-?\\d+$" ) )
{
tempNum[numCnt] = meMo[i];
numCnt++;
}
else
{
tempStr[strCnt] = meMo[i];
strCnt++;
}
}

//モード判定、操作
if( mode == ASC_SORT )
{
//文字列をunicodeの順番でソート
Arrays.sort( tempStr , cmp );

//文字列を長さでソート
Arrays.sort( tempStr , len );

//数字でソート
Arrays.sort( tempNum , num ); //臨時容器にソートした要素を加わる
System.arraycopy(tempNum,0,meMo,0,cnt); System.arraycopy(tempStr,0,meMo,cnt,memoLen-cnt);

//ソートした要素を上書き
a21.set_BASARAY(meMo);
}

if( mode == DESC_SORT )
{
//文字列をunicodeの順番でソート
Arrays.sort( tempStr , cmpRev );

//数字でソート
Arrays.sort( tempNum , numRev );

//文字列を長さでソート
Arrays.sort( tempStr , lenRev );

//臨時容器にソートした要素を加わる
System.arraycopy(tempNum,0,meMo,memoLen-cnt,cnt);

System.arraycopy(tempStr,0,meMo,0,memoLen-cnt);

//ソートした要素を上書き
a21.set_BASARAY(meMo);
}
}

}

解决方案 »

  1.   

    好吧没背过专业词汇就凭感觉写了
    多谢指教~还是想听听看有没有更好的方案解决数字字母混合排列
    百无聊赖地在网上找了找,几乎没正确的
    有说按hashcode的,有说按ascii码的,还有什么replace之后排的
    现在写的这个自觉粗糙,求前辈们指导下思路
      

  2.   

    使用ascci码 处理下你的小数点位数的 然后写判断大小的函数 在使用排序算法
      

  3.   

    楼上能说得详细点么,ascii处理小数点是处理什么呢?