0

I need to sort my list according to the "Prioridade" attribute, and after that, I need to put all the elements isConsumido() = true to the end of the list, how would I do that?

I tried this, but is looped.

public void ordenar(List<ItemCultural> array) {
    int i;
    boolean a = true;
    while (a) {
        a = false;
        for (i = 0; i < array.size() - 1; i++) {
            if (array.get(i).getPrioridade() < array.get(i + 1).getPrioridade()) {
                Collections.swap(array, i, i + 1);
                a = true;
            }
        }
    }

    a = true;
    while (a){
        a = false;
        for (i = 0; i < array.size() - 1; i++) {
            if (array.get(i).isConsumido()) {
                Collections.swap(array, i, array.size()-1);
                a = true;
            }
        }
    }
}

getPrioridade() returns a int, and isConsumido returns true or false. Thank you!

Breno Henrique
  • 81
  • 1
  • 10
  • 1
    Why do the second bit *after* sorting the list rather than *during*? – T.J. Crowder Mar 05 '16 at 15:46
  • @T.J.Crowder i don't understand your question, sorry. – Breno Henrique Mar 05 '16 at 15:52
  • Just wanted to post an answer, but now it's already closed. However, answer is short enough to put in a comment: Don't implement your own sort, just use Java 8's sorting and comparing API: `Collections.sort(array, Comparator .comparing(ItemCultural::isConsumido) .reversed() .thenComparing(ItemCultural::getPrioridade) .reversed());` – tobias_k Mar 05 '16 at 16:08

3 Answers3

0

There's no need to sort twice, just implement Comparator or similar based on the two criteria you have: The major criterion is isConsumido (putting ones where that's true at the end), the secondary criterion is the relative prioridade:

yourList.sort(new Comparator<ItemCultural>() {
    public int compare(ItemCultural a, ItemCultural b) {
        int result;
        if (a.isConsumido()) {
            result = b.isConsumido() ? 0 : 1;
        } else {
            result = b.isConsumido() ? -1 : 0;
        }
        if (result == 0) {
            result = a.getPrioridade() - b.getPrioridade();
        }
        return result;
    }
});

Live Example

(In the above I've used yourList rather than array as the name of the variable. Calling a variable referring to a List "array" is both odd and misleading.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Simply you can try removing and adding element back to the list.

it will push element to the end of the list and there is no need of while loop:

[EDIT]

int k = 0;
for (i = 0; i < array.size()-k; i++) {
            if (array.get(i).isConsumido()) {
                ItemCultural item = array.remove(i);
                array.add(item);
                k++;
                i--;
            }
}
PyThon
  • 1,007
  • 9
  • 22
  • I don't think this will work since you're using index instead of iteraterors, if you remove the item at position x the item at position x+1 will be moved to position x. Therefore you will miss some of the items. – Petter Mar 05 '16 at 16:02
  • When an item is removed , all other elements will be pushed to the left and the removed item is added to the end. No item will be missed, arraylist will always have the same size. – PyThon Mar 05 '16 at 16:05
  • @Petter- Thanks for correcting, edited as per your comment, Can you check now. – PyThon Mar 05 '16 at 16:18
  • @PyThon this algorithm partially works. Example: I added one item with priority 1, added one item with priority 2, and added another with priority 3. Turns item who have priority 2, and turns item who have priority 3, the top of the list showing a blank space, which should have gone to the end of the list. Thank you bro! – Breno Henrique Mar 05 '16 at 16:25
  • did you tried the edited version? – PyThon Mar 05 '16 at 16:27
  • @PyThon worked perfectly, thank you! – Breno Henrique Mar 05 '16 at 16:31
-1

I would implement Comparable in ItemCultural and use Collections.sort(array). I'm having trouble understanding the "isConsumido() = true" portion of code.

neal
  • 880
  • 6
  • 11
  • I need to order by "prioridade" and after, I need to put all elements who have isConsumido() = true in the end of the list. Consumido is one attribute of ItemCultural. isConsumido() is your getter. – Breno Henrique Mar 05 '16 at 15:55
  • You might want to rethink your implementation. It sounds as if you'll be doing a lot of sorting and looping through that list to check if something's been consumed. You could implement comparable in ItemCultural and then use PriorityQueue data structure to queue up the items to be processed. When isConsumid() is true then put the objects in another data structure of processedRecords. – neal Mar 05 '16 at 16:05