论坛首页 Java版

中文按笔画排序

浏览 945 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-11-07 关键字: java mysql 中文按笔画排序

最近在做一个的项目。用到的笔画排序,客户环境:数据库:linux+mysql  . java实现。

 

package com.accessmedia.ingestion.junit;

import java.util.Comparator;

public class ArrayComparator implements Comparator{
 
 /** 要u25490 序u30340 列u19979 标 */  
 private int keyColumn = 0;
 
 /** 排u24207 方u27861 ,为u21319 序u-248 默u-29788 )u-244 -1为u-27059 序 */
 private int sortOrder = 1;
 public ArrayComparator () {}
 public ArrayComparator (int keyColumn) {
      this.keyColumn = keyColumn;
 }
 public ArrayComparator (int keyColumn,int sortOrder) {
  this.keyColumn = keyColumn;
  this.sortOrder = sortOrder;
 }
 public int compare(Object a, Object b) {
  if (a instanceof String[]) {
   return sortOrder * ((String[])a)[keyColumn].compareTo(((String[])b)[keyColumn]);
  } else if (a instanceof int[]){
   return sortOrder * (((int[])a)[keyColumn] - ((int[])b)[keyColumn]);
        } else {
         return 0;
        }
 }
}

 

 

 

package com.accessmedia.ingestion.junit;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

 


public class Test {
 
 public static java.sql.Connection connectToDb(String hostName, String databaseName)throws Exception
 {
         Connection connection=null;
         String connName = "jdbc:mysql://"+hostName+":3306/"+databaseName+"?user=root&charset=utf8";
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         connection = DriverManager.getConnection(connName);
         return connection;
 }
 
 public static String getChineseID(String unicode){
  Connection conn=null;
     ResultSet rs=null;
  Statement stmt=null;
  try {
   String sql = "select * from cnword as c where c.unicode ='"+unicode+"'";
   conn=Test.connectToDb("localhost","test");
   stmt = conn.createStatement();
   
   rs = stmt.executeQuery(sql);
   while(rs.next()){
    System.out.println(rs.getString("id")+"-----"+rs.getString("unicode"));
              return rs.getString("id");
         }  
//   return String.valueOf(rs.getInt(1));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    stmt.close();
    rs.close();
    conn.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
  return "";
  
 }

 
 public void sort(String [][] obj) {
  String [] tmparr = new String[3];
        for (int i = 0 ;i < obj.length ;i++ ) {   
         for (int j = 0 ;j < obj.length - i - 1 ;j++ ){   
                  if (obj[j][1].compareTo(obj[j + 1][1]) > 0) { 
                   tmparr = obj[j];
                      obj[j] = obj[j + 1];
                      obj[j + 1] = tmparr;
                  }
            }
        }
        for (int i = 0; i < obj.length; i++) {
   String string = obj[i][1];
   System.out.println(i+"string---"+string);
  }
    }
 public static void printArray(String[][] arr) {
      for (int i= 0; i< arr.length ; i++) {
       for (int j = 0; j < arr[i].length; j++) {
        System.out.print(arr[i][j] +"\t");
       }
       System.out.println();
      }
      System.out.println("======================");
 }


 public static void main(String[] args) {
  
//  String str="價1a火灬A";
  String [][] str8= new String[][]{{"1","火一灬齾",""},{"1","1齾11",""},{"1","一齾火灬",""},{"1","丁灬一",""},{"1","1火一灬",""},{"1","丨一火灬",""},{"1","一丨火灬",""}};
//  char[]  cc   = str.toCharArray();
  
  for (int j = 0; j < str8.length; j++) {
   String unicodeName="";
   String name = str8[j][1].toString();
   char[]  cc   = name.toCharArray();
   for (int i = 0; i < cc.length; i++) {
    char c = cc[i];
    String temp ="";
    int   value   =   (int)c;
    if(value>=19968 && value <=40869){
     temp = Test.getChineseID(String.valueOf(value));
    }else{
     temp = String.valueOf(value);
    }
    
    int leg = temp.length();
    switch (leg){
     case 1:
      temp ="0000"+temp;
      break;
     case 2:
      temp ="000"+temp;
      break;
     case 3:
      temp ="000"+temp;
      break;
     case 4:
      temp ="0"+temp;
      break;
    }  
     
      if(temp.length()==1) temp ="000"+temp;
      if(temp.length()==2) temp ="00"+temp;
      if(temp.length()==3) temp ="0"+temp;
     
      unicodeName += temp;
   }
   str8[j][2]=unicodeName;
   
  }
  
  Arrays.sort(str8, new ArrayComparator(2));
  for (int i = 0; i < str8.length; i++) {
   System.out.println(str8[i][0]+"---"+str8[i][1]+"---"+str8[i][2]);
  }
 }
 
}

   
最后更新时间:2007-10-31
有用。
但是别人需要的不是你的代码,而是你的想法或做法
   
0 请登录后投票
最后更新时间:2007-11-02
谢谢上面大哥的提醒,下面是我的想法:http://jonas.javaeye.com/admin/show/137589
   
0 请登录后投票
最后更新时间:2007-11-02
重新做一个UNICODE``??

把里面中文的部分自己按笔画排的序?

这真不容易`!!
   
0 请登录后投票
论坛首页 Java版

跳转论坛:
JavaEye推荐