private static String __ENCODE__ = "GBK"; // 一定要是GBK
private static String __SERVER_ENCODE__ = "GB2312"; // 服务器上的缺省编码
private List<Company> getCompanyList(List<Company> companyList) {
//排序
for(int i=companyList.size()-1;i>1;i--){
for (int j = 0; j < i; j++) {
String m_s1 = null, m_s2 = null;
int res = 0;
try {
// 先将两字符串编码成GBK
m_s1 = new String(companyList.get(i).getName().getBytes(__SERVER_ENCODE__), __ENCODE__);
m_s2 = new String(companyList.get(j).getName().getBytes(__SERVER_ENCODE__), __ENCODE__);
} catch (UnsupportedEncodingException e) {
res = companyList.get(i).getName().compareTo(companyList.get(j).getName());
}
res = chineseCompareTo(m_s1, m_s2);
if (res < 0) {
Company tmp = companyList.get(i);
companyList.set(i, companyList.get(j));
companyList.set(j, tmp);
}
}
}
return companyList;
}
// 获取一个汉字/字母的Char值
public static int getCharCode(String s) {
if (s == null)
return -1; // 保护代码
byte[] b = s.getBytes();
int value = 0;
// 保证取第一个字符(汉字或者英文)
for (int i = 0; i < b.length && i <= 2; i++) {
value = value * 100 + b[i];
}
return value;
}
//比较两个字符串 (只使用一个字符进行匹配,所以)
public int chineseCompareTo(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
int n = Math.min(len1, len2);
// for (int i = 0; i < n; i++) {
int s1_code = getCharCode(s1.charAt(0) + "");
int s2_code = getCharCode(s2.charAt(0) + "");
// if (s1_code != s2_code)
return s1_code - s2_code;
// }.
// return len1 - len2;
}
private static String __SERVER_ENCODE__ = "GB2312"; // 服务器上的缺省编码
private List<Company> getCompanyList(List<Company> companyList) {
//排序
for(int i=companyList.size()-1;i>1;i--){
for (int j = 0; j < i; j++) {
String m_s1 = null, m_s2 = null;
int res = 0;
try {
// 先将两字符串编码成GBK
m_s1 = new String(companyList.get(i).getName().getBytes(__SERVER_ENCODE__), __ENCODE__);
m_s2 = new String(companyList.get(j).getName().getBytes(__SERVER_ENCODE__), __ENCODE__);
} catch (UnsupportedEncodingException e) {
res = companyList.get(i).getName().compareTo(companyList.get(j).getName());
}
res = chineseCompareTo(m_s1, m_s2);
if (res < 0) {
Company tmp = companyList.get(i);
companyList.set(i, companyList.get(j));
companyList.set(j, tmp);
}
}
}
return companyList;
}
// 获取一个汉字/字母的Char值
public static int getCharCode(String s) {
if (s == null)
return -1; // 保护代码
byte[] b = s.getBytes();
int value = 0;
// 保证取第一个字符(汉字或者英文)
for (int i = 0; i < b.length && i <= 2; i++) {
value = value * 100 + b[i];
}
return value;
}
//比较两个字符串 (只使用一个字符进行匹配,所以)
public int chineseCompareTo(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
int n = Math.min(len1, len2);
// for (int i = 0; i < n; i++) {
int s1_code = getCharCode(s1.charAt(0) + "");
int s2_code = getCharCode(s2.charAt(0) + "");
// if (s1_code != s2_code)
return s1_code - s2_code;
// }.
// return len1 - len2;
}