对2D数组进行排序,数字应该是连续的

时间:2014-03-12 23:23:51

标签: java arrays sorting 2d

我制作了一个现在看起来像这样的2D数组:

(3,0)
(0,1)
(3,1)
(0,2)
(3,2)
(0,3)
(1,3)
(2,3)
(3,3)

问题是,我想知道如何对二维数组进行排序,以便得到这样的结果:

(0,0)
(0,1)
(0,2)
(0,3)
(1,3)
(2,3)
(3,3)
(3,2)
(3,1)
(3,0)

每个坐标在坐标之前或之后至少有一个相同的数字。

编辑:

我想为简短的信息道歉。无论如何,我尝试了@khaled_gomaa的代码并且它没有提供我想要的输出,但我很感激他的帮助!

import java.util.Arrays; import java.util.Comparator;

public class Asdf {

    public static void main(final String[] args) {
        final int[][] data = new int[][] { new int[] { 0, 0 },
                new int[] { 3, 0 }, new int[] { 0, 1 }, new int[] { 3, 1 },
                new int[] { 0, 2 }, new int[] { 3, 2 }, new int[] { 0, 3 },
                new int[] { 1, 3 }, new int[] { 2, 3 }, new int[] { 3, 3 } };

        for (int[] indiv : data) {
            System.out.println("(" + indiv[0] + "," + indiv[1] + ")");
        }

        Arrays.sort(data, new Comparator<int[]>() {
            @Override
            public int compare(final int[] entry1, final int[] entry2) {
                if (entry1[0] == entry2[0]) {
                    if (entry1[1] > entry2[1]) {
                        return 1;
                    } else {
                        return -1;
                    }
                } else {
                    if (entry1[0] > entry2[0]) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            }
        });

        System.out.println();

        for (int i = 0; i < data.length; i++) {
            int indiv[] = data[i];
            System.out.println("(" + indiv[0] + "," + indiv[1] + ")");
        }
    }
}

输出:

(0,0)
(0,1)
(0,2)
(0,3)
(1,3)
(2,3)
(3,0)
(3,1)
(3,2)
(3,3)

3 个答案:

答案 0 :(得分:0)

import java.util.Arrays;
import java.util.Comparator;

public class Asdf {

  public static void main(final String[] args) {
    final int[][] data = new int[][]{
    new int[] {3,0},
    new int[] {0,1},
    new int[] {3,1},
    new int[] {0,2},
    new int[] {3,2},
    new int[] {0,3},
    new int[] {1,3},
    new int[] {2,3},
    new int[] {3,3}
    };

    Arrays.sort(data, new Comparator<int[]>() {
        @Override
        public int compare(final int[] entry1, final int[] entry2) {
            if(entery1[0] == entry2[0]){
              if(entery1[1] > entery2[1])
                return 1;
              else
                return -1;
            }
            else{
              if(entery1[0] > entery2[0])
                return 1;
              else
                return -1;
            }

            final String time2 = entry2[0];
            return time1.compareTo(time2);
        }
    });
}

}

答案 1 :(得分:0)

为什么不制作2个单维数组然后......分别比较每个?

之后你应该把它们放在一起创建一个2d数组。

我这样说是因为你似乎正在单独比较“列”。我的意思是说你没有将每个索引中的坐标保持在一起。无论如何,每列的数据都是分开的。

答案 2 :(得分:0)

按您的意愿对其进行排序。

它使用名为TwoIntsForCompare的类,它实现Comparable。它将索引零值(我称之为&#34; major&#34;)的权重比&#34; minor&#34;大十倍。 (指数1)价值。

class TwoIntsForCompare implements Comparable<TwoIntsForCompare>  {
   public final int major;
   public final int minor;
   public TwoIntsForCompare(int[] idx0Major_idx1Minor)  {
      this.major = idx0Major_idx1Minor[0];
      this.minor = idx0Major_idx1Minor[1];
   }
   public int compareTo(TwoIntsForCompare to_compareTo)  {
      return  (((major - to_compareTo.major) * 10) +
         (minor - to_compareTo.minor));
   }
   public int[] getArray()  {
      return  (new int[] {major, minor});
   }
}

将每个子数组放入此对象后,将它们插入到TreeMap中,并使用TwoIntsForCompare s compareTo函数自动进行排序。

主要逻辑:

Map<TwoIntsForCompare,Object> twoIntsMap = new TreeMap<TwoIntsForCompare,Object>();
for(int i = 0; i < intArrArrOrig.length; i++)  {
   twoIntsMap.put(new TwoIntsForCompare(intArrArrOrig[i]), null);
}

int[][] intArrArrSorted = new int[intArrArrOrig.length][2];
Set<TwoIntsForCompare> tifcSet = twoIntsMap.keySet();
int idx = 0;
Iterator<TwoIntsForCompare> itrTifc = tifcSet.iterator();
while(itrTifc.hasNext())  {
   intArrArrSorted[idx++] = itrTifc.next().getArray();
}

完整代码:

import  java.util.Set;
import  java.util.Iterator;
import  java.util.TreeMap;
import  java.util.Map;
/**
   <P>{@code java SortArrayOfTwoInts}</P>
 **/
public class SortArrayOfTwoInts  {
   public static final void main(String[] ignored)  {
      int[][] intArrArrOrig = new int[][]  {
         new int[] {3,0},
         new int[] {0,1},
         new int[] {3,1},
         new int[] {0,2},
         new int[] {3,2},
         new int[] {0,3},
         new int[] {1,3},
         new int[] {2,3},
         new int[] {3,3}};

      System.out.println("Original:");
      for(int i = 0; i < intArrArrOrig.length; i++)  {
         System.out.println("[" + intArrArrOrig[i][0] + ", " + intArrArrOrig[i][3] + "]");
      }

      System.out.println();

      Map<TwoIntsForCompare,Object> twoIntsMap = new TreeMap<TwoIntsForCompare,Object>();
      for(int i = 0; i < intArrArrOrig.length; i++)  {
         twoIntsMap.put(new TwoIntsForCompare(intArrArrOrig[i]), null);
      }

      int[][] intArrArrSorted = new int[intArrArrOrig.length][2];
      Set<TwoIntsForCompare> tifcSet = twoIntsMap.keySet();
      int idx = 0;
      Iterator<TwoIntsForCompare> itrTifc = tifcSet.iterator();
      while(itrTifc.hasNext())  {
         intArrArrSorted[idx++] = itrTifc.next().getArray();
      }

      System.out.println("Sorted:");
      for(int i = 0; i < intArrArrSorted.length; i++)  {
         System.out.println("[" + intArrArrSorted[i][0] + ", " + intArrArrSorted[i][4] + "]");
      }
   }
}
class TwoIntsForCompare implements Comparable<TwoIntsForCompare>  {
   public final int major;
   public final int minor;
   public TwoIntsForCompare(int[] idx0Major_idx1Minor)  {
      this.major = idx0Major_idx1Minor[0];
      this.minor = idx0Major_idx1Minor[1];
   }
   public int compareTo(TwoIntsForCompare to_compareTo)  {
      return  (((major - to_compareTo.major) * 10) +
         (minor - to_compareTo.minor));
   }
   public int[] getArray()  {
      return  (new int[] {major, minor});
   }
}

输出:

[C:\java_code\]java SortArrayOfTwoInts
Original:
[3, 0]
[0, 1]
[3, 1]
[0, 2]
[3, 2]
[0, 3]
[1, 3]
[2, 3]
[3, 3]

Sorted:
[0, 1]
[0, 2]
[0, 3]
[1, 3]
[2, 3]
[3, 0]
[3, 1]
[3, 2]
[3, 3]