-1

I wanted to sort the items in recyclerView. I have usernames such as 20ABC1, 20ABC2,..., 20ABC10,..etc.

I have tried answers from the related questions one of which was:

public static final Comparator<Users> BY_NAME_ALPHABETICAL = (users, t1) -> users.Username.compareTo(t1.Username);

But this does not solve the problem exactly. 20ABC10, 20ABC11,...20ABC19 comes above 20ABC2. I think it is because it checks character by character.

Any way I can solve this?

Thank you :)

Ashish Yadav
  • 543
  • 2
  • 7
  • 30
  • Possible duplicate of [Natural sort order string comparison in Java - is one built in?](https://stackoverflow.com/questions/1262239/natural-sort-order-string-comparison-in-java-is-one-built-in) – karan Apr 16 '19 at 05:15
  • you need to search natural sort there is implementation for comparator available. check this https://stackoverflow.com/questions/1262239/natural-sort-order-string-comparison-in-java-is-one-built-in – karan Apr 16 '19 at 05:15
  • 1
    Although @KaranMer you are correct. But my type of issue can be solved by this answer by Bohemian https://stackoverflow.com/questions/13973503/sorting-strings-that-contains-number-in-java – Ashish Yadav Apr 16 '19 at 05:43

4 Answers4

1

My Issue got solved by using this answer Sorting Strings that contains number in Java by Bohemian where he removed the alphabets from the strings and compared the remaining ints

Collections.sort(strings, new Comparator<String>() {
    public int compare(String o1, String o2) {
        return extractInt(o1) - extractInt(o2);
    }

    int extractInt(String s) {
        String num = s.replaceAll("\\D", "");
        // return 0 if no digits found
        return num.isEmpty() ? 0 : Integer.parseInt(num);
    }
});
Ashish Yadav
  • 543
  • 2
  • 7
  • 30
1
public Observable<User> getUsersWithBlogs() {
return Observable.fromIterable(UserCache.getAllUsers())
.filter(user -> user.blog != null && !user.blog.isEmpty())
.sorted((user1, user2) -> user1.name.compareTo(user2.name));
}
0

put this in your adapter

void sortByName(boolean isDescending) {
    if (mDataList.size() > 0) {
        Collections.sort(mDataList, new Comparator<Users>() {
            @Override
            public int compare(Users object1, Users object2) {
                if (isDescending)
                    return object2.getUsername().toLowerCase().compareTo(object1.getUsername().toLowerCase().trim());
                else
                    return object1.getUsername().toLowerCase().compareTo(object2.getUsername().toLowerCase().trim());
            }
        });
        notifyDataSetChanged();
    }
}

Then use it like this:

adapter.sortByName( true||false );
S.R
  • 2,819
  • 2
  • 22
  • 49
0

Using kotlin,

val comp: Comparator<Users> = Comparator { o1, o2 -> o1.Username.trim().compareTo(o2.Username.trim()) }
Collections.sort(users, comp)

Do the same for java.

Shaon
  • 2,496
  • 26
  • 27
  • It should solve the problem. I think you are doing something wrong. try tolowercase() – Shaon Apr 16 '19 at 04:56
  • So by your solution what should come up 20ABC10 or 20ABC2? For me 20ABC10 is showing up and thats the problem. Otherwise normally the sorting is happening just fine. – Ashish Yadav Apr 16 '19 at 05:00