简体   繁体   中英

Java Array sort by means of a comparator

The complete code can be found at: https://skydrive.live.com/redir?resid=7B7D2F11B13EF9C9!54468&authkey=!AD4fD8sGgc7oJIE

This is part of the code:

g_sorted = 0;
for (int l_loop = 0; l_loop < l_length; l_loop++)
{
    if (!l_IntegerArray[l_loop].equals(g_exclude))
    {
        g_tag[g_sorted] = l_loop;
        g_tosort_Integer[g_sorted] = l_IntegerArray[l_loop];
        g_sorted++;
    }
} // for (int l_loop = 0; l_loop < p_toSort; l_loop++)

Arrays.sort
(g_tag, 0, g_sorted, new Comparator<Integer>()
    {
        public int compare(Integer i1, Integer i2)
        {
            return Integer.compare(g_tosort_Integer[i1], g_tosort_Integer[i2]);
        }
    }
);

g_tosort_Integer, g_tag, g_tosort_Integer are 'Integer'. g_exclude is used to exclude items which must not be part of the sort.

When no items are excluded (no item has the value equal to g_exclude or the if-statement is commented), everything works fine.

When 1 or more items are excluded I get a NullPointerException:

Exception in thread "main" java.lang.NullPointerException
at TestSort$1.compare(TestSort.java:53)
at TestSort$1.compare(TestSort.java:50)
at java.util.TimSort.binarySort(TimSort.java:265)
at java.util.TimSort.sort(TimSort.java:190)
at java.util.Arrays.sort(Arrays.java:727)
at TestSort.<init>(TestSort.java:48)
at TestSort.main(TestSort.java:71)

Can someone explain this to me ? Thanks.

This is how to solve. I kept your class "structure", but I took the liberty of changing variable names to get clearer what I was doing. You asked how to order an array not changing the order of elements, but changing a index upon it.

Here's the code.

    public class TestSort {

       int c_maxSort = 10000;
       Integer[] unsortedAndFiltered = new Integer[c_maxSort];
       Integer[] index = new Integer[c_maxSort];

       public TestSort() {

          Integer exclude = 0;
          int newPosition;
          int lengthOfOriginalArray;


          Integer[] originalArray = { 5, 3, 0, 2, 7, 1, 5, 6, 8, 4 };
          lengthOfOriginalArray = originalArray.length;

          System.out.print("Original: ");
          for (int i = 0; i < lengthOfOriginalArray; i++)
             System.out.print(originalArray[i] + " ");
          System.out.println(" - Length: " + lengthOfOriginalArray);

          newPosition = 0;

          for (int i = 0; i < lengthOfOriginalArray; i++) {

             if (!originalArray[i].equals(exclude)) {
                index[newPosition] = newPosition;
                unsortedAndFiltered[newPosition] = originalArray[i];
                newPosition++;
             }
          }

          System.out.println("Tags: ");
          for (int i = 0; i < newPosition; i++)
             System.out.print(index[i] + " ");
          System.out.println("");
          System.out.println("Unsorted numbers: ");
          for (int l_loop = 0; l_loop < newPosition; l_loop++)
             System.out.print(unsortedAndFiltered[l_loop] + " ");
          System.out.println("");

          int deleted = lengthOfOriginalArray - newPosition;
          Arrays.sort(index, 0, newPosition, new IndirectedComparator(unsortedAndFiltered, index));

          System.out.println("Sorted Tags: ");
          for (int i = 0; i < newPosition; i++)
             System.out.print(index[i] + " ");
          System.out.println("");
          System.out.println("Sorted Numbers: ");
          for (int i = 0; i < newPosition; i++)
             System.out.print(unsortedAndFiltered[index[i]] + " ");
          System.out.println("");

       }

       public static void main(String[] args) {
          new TestSort();
       }

    }

where the comparator is coded like this:

public class  IndirectedComparator implements Comparator<Integer> {


   private Integer[] array;
   private Integer[] index;

   public IndirectedComparator(Integer [] array, Integer[] index){
      this.array = array;

      this.index = new Integer[index.length];
      System.arraycopy(index, 0, this.index, 0, index.length);
   }

   @Override
   public int compare(Integer i1, Integer i2) {
      return Integer.compare(array[index[i1]], array[index[i2]]);
   }

}

This is my execution:

Original: 5 3 0 2 7 1 5 6 8 4  - Length: 10
Tags: 
0 1 2 3 4 5 6 7 8 
Unsorted numbers: 
5 3 2 7 1 5 6 8 4 
Sorted Tags: 
4 2 1 8 0 5 6 3 7 
Sorted Numbers: 
1 2 3 4 5 5 6 7 8 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM