ISchwarz23 / SortableTableView

An Android library containing a simple TableView and an advanced SortableTableView providing a lot of customisation possibilities to fit all needs.
Apache License 2.0
1.05k stars 237 forks source link

sort table except last row #161

Closed W0nderL4ndz closed 6 years ago

W0nderL4ndz commented 6 years ago

hi, I want to sort rows by a column except last row , how can i do this ?

thanks in advance

ISchwarz23 commented 6 years ago

Hi @DanVilGui,

What type of data do you have in your table? String arrays or custom objects?

Best regards, Ingo

W0nderL4ndz commented 6 years ago

Custom objects @ISchwarz23

ISchwarz23 commented 6 years ago

Can you distinguish the last object from the other objects? (Except of it's index)

W0nderL4ndz commented 6 years ago

yes I have a column "name" and the values are "name01", "name02", "name 03" , the last row is "Total". when I order by "name" , "Total" moves.

ISchwarz23 commented 6 years ago

So write your own comparator like this:

public class TotalAwareComparator implements Comparator<YourObject> {

    private final SortableTableView tableView;

    public TotalAwareComparator(SortableTableView tableView) {
        this.tableView = tableView;
    }

    public int compare(YourObject obj1, YourObject obj2) {
        if(obj1.isTotal()) {
            return tableView.getSortState().getSortedOrder() == SortingOrder.ASCENDING ? 1 : -1;
        } else if(obj2.isTotal()) {
            return tableView.getSortState().getSortedOrder() == SortingOrder.ASCENDING ? -1 : 1;
        } else {
            // do normal sorting
        }
    }

}

I wrote this code on my mobile so there may are some errors. Also I may swapped the return values for the "total objects".

W0nderL4ndz commented 6 years ago

thank you so much