Pooran Kharol
Pooran Kharol

Reputation: 58

Sorting List by Title (1. Name,11. Name,2. Name) in RecyclerView Android

I used collections to sort but sorting is incorrect with (1. Name,11. Name,2. Name) case. I have tried my solution my unable to achieve anything but without the number in String, it is working fine and correctly sorting.

private void StudentDataPrepare() {
        studentData data=new studentData("1. Layout",25);
        studentDataList.add(data);
        data=new studentData("10. Absolute",25);
        studentDataList.add(data);
        data=new studentData("11. Applying",20);
        studentDataList.add(data);
        data=new studentData("12. Exec",28);
        studentDataList.add(data);
        data=new studentData("13. Exec",15);
        studentDataList.add(data);
        data=new studentData("2. Basics",19);
        studentDataList.add(data);
        data=new studentData("3. AlignItems",52);
        studentDataList.add(data);
        data=new studentData("4. Flex",30);
        studentDataList.add(data);
        data=new studentData("5. Justify",28);
        studentDataList.add(data);
        data=new studentData("6. Flex",28);
        studentDataList.add(data);
        data=new studentData("7. Align",28);
        studentDataList.add(data);
        data=new studentData("8. The",28);
        studentDataList.add(data);
        data=new studentData("9. Top",28);
        studentDataList.add(data);
    }
 public void sortAsc() {
        Collections.sort(studentDataList, new Comparator<studentData>() {
            @Override
            public int compare(studentData studentData, studentData t1) {
                return studentData.name.compareToIgnoreCase(t1.name);
            }
        });
        notifyDataSetChanged();
    }
    public void sortDesc() {
        Collections.sort(studentDataList, new Comparator<studentData>() {
            @Override
            public int compare(studentData studentData, studentData t1) {
                return t1.name.compareToIgnoreCase(studentData.name);
            }
        });
        notifyDataSetChanged();
    }

ORDER

Upvotes: 1

Views: 52

Answers (1)

aminography
aminography

Reputation: 22832

The problem is that your code tries to sort the items in alphabetical sort order. What you are looking for is the natural sort order. Refer to https://en.wikipedia.org/wiki/Natural_sort_order for more information.

However, the solution could be using a custom comparator, like https://github.com/paour/natorder/blob/master/NaturalOrderComparator.java which helps you to achieve the desired result.


If you are always sure that the studentData.name follows the pattern of A_NUMBER.SOME_STRING, you can use the following approach which is simpler:

public void sortAsc() {
    Collections.sort(studentDataList, new Comparator<studentData>() {
        @Override
        public int compare(studentData studentData, studentData t1) {
            int left = Integer.parseInt(studentData.name.substring(0, studentData.name.indexOf('.')));
            int right = Integer.parseInt(t1.name.substring(0, t1.name.indexOf('.')));
            return left - right;
        }
    });
    notifyDataSetChanged();
}

public void sortDesc() {
    Collections.sort(studentDataList, new Comparator<studentData>() {
        @Override
        public int compare(studentData studentData, studentData t1) {
            int left = Integer.parseInt(t1.name.substring(0, t1.name.indexOf('.')));
            int right = Integer.parseInt(studentData.name.substring(0, studentData.name.indexOf('.')));
            return left - right;
        }
    });
    notifyDataSetChanged();
}

Upvotes: 1

Related Questions