Sorting Java arrays based on multiple parameters

I have an array that I want to sort in ascending order. However, I want to sort them with a boolean array reference. I would like to sort the values ​​that are true

in ascending order and then the values false

in ascending order. A bit stuck on how to get there.

This is what I have:

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);

      

I need the output:

1 2 6 3 4 5

+3


source to share


2 answers


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 function compareTo(Item otherItem)

based on the order you want.
Once this is done, all you have to do is call Arrays.sort()

on your array Item

.

Version 2 (no compare / 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


source


(To enlarge my comment:

You need a basic "thing":

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

      

... and comparable ...



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;
    }
}

      

(Note that t1.newAvailable == true is redundant, but I think it clarifies what's going on here.)

Now create an array Thing and call Arrays.sort (Thing [] things, CompareThings);

0


source







All Articles