Java Array基于多个参数的排序

时间:2015-06-02 00:07:52

标签: java arrays sorting

我有一个数组,我想按升序排序。但是,我想通过引用布尔数组对它们进行排序。我想按升序对true的值进行排序,然后按升序排列false的值。 很少停留在如何到达那里。

这就是我目前的情况:

Object[] arr = new Object[6];

arr[0] = new Object(2); 
arr[1] = new Object(5); 
arr[2] = new Object(3); 
arr[3] = new Object(1);
arr[4] = new Object(6);
arr[5] = new Object(4);

Available[] avalarr = new Available[6];

availarr[0] = new Available (true);
availarr[1] = new Available (false);
availarr[2] = new Available (false);
availarr[3] = new Available (true);
availarr[4] = new Available (true);
availarr[5] = new Available (false);

我需要输出:

1 2 6 3 4 5

2 个答案:

答案 0 :(得分:1)

Code:

import java.util.Arrays;

public class SelectiveSort {

    public static void main(String[] args) {

        Item [] items = new Item [6];
        items[0] = new Item(2, true);
        items[1] = new Item(5, false);
        items[2] = new Item(3, false);
        items[3] = new Item(1, true);
        items[4] = new Item(6, true);
        items[5] = new Item(4, false);

        System.out.println("Before Sorting:");
        // Removed enhanced for loop
        for(int i = 0; i < items.length; i++) {
            System.out.print(items[i].getIntValue() + " ");
        }

        // Sorting
        Arrays.sort(items);

        System.out.println("\n\nAfter Sorting:");
        // Removed enhanced for loop
        for(int i = 0; i < items.length; i++) {
            System.out.print(items[i].getIntValue() + " ");
        }
        System.out.println();
    }
}

class Item implements Comparable<Item> {

    private int _intValue;
    private boolean _boolValue;

    public Item(int intValue, boolean boolValue) {
        _intValue = intValue;
        _boolValue = boolValue;
    }

    public int getIntValue() { return _intValue; }

    public boolean getBoolValue() { return _boolValue; }

    @Override
    public int compareTo(Item otherItem) {

        // Using explicit comparison
        int boolComparison = (_boolValue == otherItem._boolValue) ? 0 :
            (_boolValue) ? 1 : -1;
        return (boolComparison != 0) ? -boolComparison :
            ( (_intValue == otherItem.getIntValue()) ? 0 :
                (_intValue > otherItem.getIntValue()) ? 1 : -1);
    }
}

Output:

Before Sorting: 2 5 3 1 6 4

After Sorting: 1 2 6 3 4 5


Explanation:

The idea is to let your "Item" implement Comparable, and override the compareTo(Item otherItem) function based on the desired order.
Once that is done, all you need to do is to call Arrays.sort() on your array of Item.

Version 2 (w/o Comparable/Comparator):

public class SelectiveSort {

    public static void main(String[] args) {

        Item [] items = new Item [6];
        items[0] = new Item(2, true);
        items[1] = new Item(5, false);
        items[2] = new Item(3, false);
        items[3] = new Item(1, true);
        items[4] = new Item(6, true);
        items[5] = new Item(4, false);

        System.out.println("Before Sorting:");
        for(int i = 0; i < items.length; i++) {
            System.out.print(items[i].getIntValue() + " ");
        }

        // Sorting
        bubbleSort(items);

        System.out.println("\n\nAfter Sorting:");
        for(int i = 0; i < items.length; i++) {
            System.out.print(items[i].getIntValue() + " ");
        }
        System.out.println();
    }

    public static void bubbleSort(Item [] items) {
        int n = items.length;
        do {
            int newN = 0;
            for(int i = 1; i < n; i++) {
                if(compareTo(items[i-1], items[i]) == 1) {
                    Item temp = items[i-1];
                    items[i-1] = items[i];
                    items[i] = temp;

                    newN = i;
                }
            }
            n = newN;
        } while (n != 0);
    }

    public static int compareTo(Item item1, Item item2) {

        int boolComparison = (item1.getBoolValue() == item2.getBoolValue())
                ? 0 : (item1.getBoolValue()) ? 1 : -1;
        return (boolComparison != 0) ? -boolComparison :
            ( (item1.getIntValue() == item2.getIntValue()) ? 0 :
                (item1.getIntValue() > item2.getIntValue()) ? 1 : -1);
    }
}

答案 1 :(得分:0)

(扩展我的评论:

你需要一个基本的东西&#34;:

class Thing {
   boolean newAvailable;
   int order;
   public Thing(boolean newAvailable, int order) {
      ...
   }
}

......和可比较......

class CompareThings implements Comparator<Thing> {
    ...
    int compare(Thing t1, Thing t2) {
        if (t1.newAvailable!=t2.newAvailable)
            return t1.newAvailable==true ? 1 : -1;
        return t1.order-t2.order;
    }
}

(请注意,t1.newAvailable == true是多余的,但我认为它澄清了这里发生了什么。)

现在构建一个Thing数组并调用Arrays.sort(Thing []的东西,CompareThings);