public class Test_1 { // 获取数组的排序规则。
// 获取该数组值按从小到大顺序,对应的顺序。
/*
 * 例如数组是: {19,12,13,16,15} 根据数组内容的大小顺序,排列出一个顺序。 即:5,1,2,4,3
 */
public static String getOrder(int array[]) {
StringBuffer order = new StringBuffer();
TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
for (int i = 0; i < array.length; i++)
h.put(array[i], i + 1); TreeMap<Integer, Integer> h_1 = new TreeMap<Integer, Integer>();
int i = 1;
for (Object o : h.keySet())
h_1.put(h.get(o), i++); for (Object o : h_1.keySet())
order.append(h_1.get(o)).append(",");
return order.toString();
}

//array 数据仓库
//order 数据仓库顺序
//return list 按照数据仓库顺序显示的数据 public static List getSort(int array[], String order) {
String s[] = order.split(",");
TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
for (int i = 0; i < array.length; i++)
h.put(array[i], i); ArrayList<Integer> a = new ArrayList<Integer>();
for (Object o : h.keySet())
a.add(h.get(o)); ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 0; i < s.length; i++)
al.add(array[a.get(Integer.parseInt(s[i]) - 1)]); return al;
} /**
 * @param args
 */
public static void main(String[] args) {
int init[] = new int[] { 10, 30, 20, 60, 50 };
String order = getOrder(init);// 获取数组从大到小排列对应的顺序
System.out.println("order=="+order);
int iArray[] = { 10, 20, 30, 50, 60 };
List sort = getSort(iArray, order);//按照规则显示iArray的数据。
System.out.println("sort=="+sort);

/*
 * 我现在的问题就是如果iArray 这个里面的数据变少了,被删除了1个到N个,
 * 但是规则却无法改变了。还是1,3,2,5,4,。那我怎么做才能在sort里面也删除
 * 掉iArray里面被删除的数据呢?
 * 
 * 即:sort现在返回【10, 30, 20, 60, 50】
 * 然后iArray数据为 【10, 20, 30, 50, 60 】 
 * 规则为:1,3,2,5,4,
 * 
 * 希望的效果:
 * 如果iArray数据为 【10, 20, 30】
 * 规则还是:1,3,2,5,4, 
 * 希望返回【10, 30, 20】
 * 
 * 
 * */


System.out.println("The End");
}}

解决方案 »

  1.   

    那如果iArray数据为 【10, 20, 30, 50】
    规则还是:1,3,2,5,4, 
    但是没有第5个数,这种情况你打算怎么处理呢?
      

  2.   

    如果只是按照规则从小到大显示,即:1,2,3,5的顺序显示:public static List getSort(int array[], String order) {
         String s[] = order.split(",");
         TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
         for (int i = 0; i < array.length; i++)
         h.put(Integer.parseInt(s[i]),array[i]);
         ArrayList<Integer> al = new ArrayList<Integer>();
         for(Object o : h.keySet())
         al.add(h.get(o));
         return al;
    }如:
    int iArray[] = { 10, 20, 30, 60 };
    输出:
    order==1,3,2,5,4,
    sort==[10, 30, 20, 60]
      

  3.   

    既然这样,你肯定需要对原来的数据仓库对比,init得传到getSort中对比,然后忽略删除了的数据的排序
      

  4.   

    http://s.click.taobao.com/t_1?i=qvFU%2BxyOVUCm&p=mm_14624551_0_0&n=11
    好东西,一起来分享哦~~
      

  5.   

    对java不是很熟悉,给你C#的算法吧!
    java里方法也类似,一定要巧用IndexOf属性!
    至于java中有没有List<int>这样的定义我不知道,
    但我确定java中肯定有 String.IndexOf()这个方法吧,
    实在不行你可以把int数组转换为String。一下代码测试通过,完全没有问题!        /// <summary>
            /// GetSortValue
            /// </summary>
            /// <param name="intValues">intValues</param>
            /// <returns>intSortValue</returns>
            public static int[] GetSortValue(int[] intValues)
            {
                //原始int数组转换为List<int>
                List<int> initList = new List<int>();
                initList = intValues.ToList();            //原始int数组转换为List<int>,并运用Sort排序
                List<int> sortList = new List<int>();
                sortList = intValues.ToList();
                sortList.Sort();            int[] intSortValue = new int[intValues.Length];            for (int i = 0; i < initList.Count; i++)
                {
                    //如果结果从1开始:需要加1,如果从0开始不加1
                    intSortValue[i] = sortList.IndexOf(initList[i]) + 1;
                }            return intSortValue;
            }
      

  6.   


    上面那个有点乱,给你个精简的:
            /// <summary>
            /// GetSortValue
            /// </summary>
            /// <param name="intValues">intValues</param>
            /// <returns>intSortValue</returns>
            public static int[] GetSortValue(int[] intValues)
            {            //原始int数组转换为List<int>,并运用Sort排序
                List<int> sortList = new List<int>();
                sortList = intValues.ToList();
                sortList.Sort();            int[] intSortValue = new int[intValues.Length];            for (int i = 0; i < intValues.Length; i++)
                {
                    //如果结果从1开始:需要加1,如果从0开始不加1
                    intSortValue[i] = sortList.IndexOf(intValues[i]) + 1;
                }            return intSortValue;
            }
      

  7.   

    如果都是数值类型就好做一些吧。
    * 如果iArray数据为 【10, 40,50, 60】
     * 规则还是:1,3,2,5,4
     * 希望返回【10, 50, 40,60】
    从规则中找到最小数值,取出最小数值的序号,把第一个数值放到最小位置的位置。第二轮循环时,规则中把刚才的最小值用某个数值替换掉,不用再去用到。
    依次第二个数值
    代码不熟,思路不知道是否正确。
      

  8.   

    应该在取得时候忽略掉不存在的数就行了。
    修改al.add(array[a.get(Integer.parseInt(s[i]) - 1)]);
    为:
    if(Integer.parseInt(s[i]<=array.length)){
       al.add(array[a.get(Integer.parseInt(s[i]) - 1)]);
    }
      

  9.   

    有几种排序方法,你看一下,希望能帮你
    package com.cucu.test; /** 
     * @author http://www.linewell.com <a href=mailto:[email protected]>[email protected]</a> 
     * @version 1.0 
     */ 
    public class Sort {   public void swap(int a[], int i, int j) { 
        int tmp = a[i]; 
        a[i] = a[j]; 
        a[j] = tmp; 
      }   public int partition(int a[], int low, int high) { 
        int pivot, p_pos, i; 
        p_pos = low; 
        pivot = a[p_pos]; 
        for (i = low + 1; i <= high; i++) { 
          if (a[i] > pivot) { 
            p_pos++; 
            swap(a, p_pos, i); 
          } 
        } 
        swap(a, low, p_pos); 
        return p_pos; 
      }   public void quicksort(int a[], int low, int high) { 
        int pivot; 
        if (low < high) { 
          pivot = partition(a, low, high); 
          quicksort(a, low, pivot - 1); 
          quicksort(a, pivot + 1, high); 
        }   }   public static void main(String args[]) { 
        int vec[] = new int[] { 37, 47, 23, -5, 19, 56 }; 
        int temp; 
        //选择排序法(Selection Sort) 
        long begin = System.currentTimeMillis(); 
        for (int k = 0; k < 1000000; k++) { 
          for (int i = 0; i < vec.length; i++) { 
            for (int j = i; j < vec.length; j++) { 
              if (vec[j] > vec[i]) { 
                temp = vec[i]; 
                vec[i] = vec[j]; 
                vec[j] = temp; 
              } 
            }       } 
        } 
        long end = System.currentTimeMillis(); 
        System.out.println("选择法用时为:" + (end - begin)); 
        //打印排序好的结果 
        for (int i = 0; i < vec.length; i++) { 
          System.out.println(vec[i]); 
        } 
        //  冒泡排序法(Bubble Sort) 
        begin = System.currentTimeMillis(); 
        for (int k = 0; k < 1000000; k++) { 
          for (int i = 0; i < vec.length; i++) { 
            for (int j = i; j < vec.length - 1; j++) { 
              if (vec[j + 1] > vec[j]) { 
                temp = vec[j + 1]; 
                vec[j + 1] = vec[j]; 
                vec[j] = temp; 
              } 
            }       } 
        } 
        end = System.currentTimeMillis(); 
        System.out.println("冒泡法用时为:" + (end - begin)); 
        //打印排序好的结果 
        for (int i = 0; i < vec.length; i++) { 
          System.out.println(vec[i]); 
        }     //插入排序法(Insertion Sort) 
        begin = System.currentTimeMillis(); 
        for (int k = 0; k < 1000000; k++) { 
          for (int i = 1; i < vec.length; i++) { 
            int j = i; 
            while (vec[j - 1] < vec[i]) { 
              vec[j] = vec[j - 1]; 
              j--; 
              if (j <= 0) { 
                break; 
              } 
            } 
            vec[j] = vec[i]; 
          } 
        } 
        end = System.currentTimeMillis(); 
        System.out.println("插入法用时为:" + (end - begin)); 
        //打印排序好的结果 
        for (int i = 0; i < vec.length; i++) { 
          System.out.println(vec[i]); 
        }     //快速排序法(Quick Sort)     Sort s = new Sort(); 
        begin = System.currentTimeMillis(); 
        for (int k = 0; k < 1000000; k++) { 
          s.quicksort(vec, 0, 5); 
        } 
        end = System.currentTimeMillis(); 
        System.out.println("快速法用时为:" + (end - begin)); 
        //打印排序好的结果 
        for (int i = 0; i < vec.length; i++) { 
          System.out.println(vec[i]); 
        } 
      } } 
    以下是运行结果: 
    选择法用时为:234 
    56 
    47 
    37 
    23 
    19 
    -5 
    冒泡法用时为:172 
    56 
    47 
    37 
    23 
    19 
    -5 
    插入法用时为:78 
    56 
    47 
    37 
    23 
    19 
    -5 
    快速法用时为:297 
    56 
    47 
    37 
    23 
    19 
    -5
      

  10.   


        public static List getSort(int array[], String order) {
            String s[] = order.split(",");
            TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
            /**/
    for (int i = 0; i < array.length; i++)
                h.put(array[i], i);        ArrayList<Integer> a = new ArrayList<Integer>();
            for (Object o : h.keySet())
                a.add(h.get(o)); ArrayList<Integer> al = new ArrayList<Integer>();
            for (int i = 0; i < s.length; i++){
    if (Integer.parseInt(s[i])<=array.length){           //判断数组边界
    al.add(array[a.get(Integer.parseInt(s[i]) - 1)]);
    }            
    }
            return al;
        }
      

  11.   

    初看一下有一个想法:用一个MAP,KEY为原数组中的值,VALUE为索引,根据这个map的kEY进行排序,再读出VALUE值。
      

  12.   


    public static List getSort(int array[], String order) {
    String s[] = order.split(","); // 将array中的元素从小到大排列,重新生成列表a
    // 在这里可以不用TreeMap的方式,写一个冒泡排序算法就行了。
    ArrayList<Integer> a = new ArrayList<Integer>();
    {
    TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
    for (int i = 0; i < array.length; i++)
    h.put(array[i], i); // a 中装入的是排好序的array
    for (Object o : h.keySet())
    a.add((Integer) o);
    } // 楼主的本意是根据排序规则重排数据仓库
    ArrayList<Integer> al = new ArrayList<Integer>();
    for (int i = 0; i < s.length; i++) {
    int index = Integer.parseInt(s[i]) - 1;
    // 如果order中排序用的索引超出数据仓库的大小,
    // 则查找下一个索引
    if (index >= a.size()) {
    continue;
    }
    al.add(a.get(index));
    }
    return al;
    }
      

  13.   

    那如果iArray数据为 【10, 20, 30, 50】 
    规则还是:1,3,2,5,4, 
    但是没有第5个数,这种情况你打算怎么处理呢?