2

Refering to this question

Sadly I cannot comment because of low reputation but I have a question relating to this solution. I want to sort ascending AND descending (on click), but keep an empty string (represents the second sort parameter) always on top of something written..

I want it like:

enter image description hereenter image description here

My code works only for ASCENDING, on descending the string is before the empty one..

SECOND_SORT {
    public int compare(String s1, String s2) {
        if (s1.equals("") && s2.equals("")) {
            return 0;
        }
        if (s1.equals("")) {
            return -1;
        }
        if (s2.equals("")) {
            return 1;
        }
        return s1.compareTo(s2);
    }

This is the sort..

if (mSortingIdentifier == SortingIdentifier.DESC) {
            Collections.sort(list, Comparator.descending(Comparator.getComparator(Comparator.FIRST_SORT, Comparator.SECOND_SORT)));
        } else {
            Collections.sort(list, Comparator.ascending(Comparator.getComparator(Comparator.FIRST_SORT, Comparator.SECOND_SORT)));
        }
Community
  • 1
  • 1
Zuop
  • 584
  • 5
  • 7
  • 21
  • Why is B before B String in descending order? – SMA Dec 17 '14 at 09:02
  • Because thats the way I want it to be - emptystring always on top of strings :D A/B and string/emptystring are two different variables of the object – Zuop Dec 17 '14 at 09:10
  • Is it really a descending then? If you need your own customized comparator then you need to write your own with your own logic and you may not call it descending. – SMA Dec 17 '14 at 09:14
  • I thought I can do it with the if(s1.equals("")) { return -1; } But I don't know why it doenst work – Zuop Dec 17 '14 at 09:18
  • Why in the second table the B->Hi is before B->Hey? Does that should not be the other way? – Dimitri Dec 17 '14 at 09:29
  • Yes, you're right but actually this doesnt matter much for me.. its the comparisson between emptystring and somestring.. – Zuop Dec 17 '14 at 10:28

1 Answers1

0

I have found a solution for me that works.. I just added another SECOND_SORT_DESC where I swap the return value.. in the adapter when I sort I check if its ASC or DESC and use the enum I want...

fyi:

SECOND_SORT_ASC {
public int compare(String s1, String s2) {
    if (s1.equals("") && s2.equals("")) {
        return 0;
    }
    if (s1.equals("")) {
        return -1;
    }
    if (s2.equals("")) {
        return 1;
    }
    return s1.compareTo(s2);
},
SECOND_SORT_DESC {
public int compare(String s1, String s2) {
    if (s1.equals("") && s2.equals("")) {
        return 0;
    }
    if (s1.equals("")) {
        return 1;
    }        // swapped these two
    if (s2.equals("")) {
        return -1;
    }
    return s1.compareTo(s2);
} // Here is where I differ between asc and desc
if (mSortingIdentifier == SortingIdentifier.DESC) {
        Collections.sort(list, Comparator.descending(Comparator.getComparator(Comparator.FIRST_SORT, Comparator.SECOND_SORT_DESC)));
    } else {
        Collections.sort(list, Comparator.ascending(Comparator.getComparator(Comparator.FIRST_SORT, Comparator.SECOND_SORT_ASC)));
    }
Zuop
  • 584
  • 5
  • 7
  • 21