How to sort listview items in descending order

后端 未结 3 1996
青春惊慌失措
青春惊慌失措 2020-12-21 14:07

So I have a listview where I wanted to sort the NumberOfRecords in descending order. I have a custom array adapter but I called my sorting class before I place a data in my

相关标签:
3条回答
  • 2020-12-21 14:13

    Okay, so I solved this by replacing the:

    p2.getNumberOfRecords().compareTo(p1.getNumberOfRecords())

    to:

    (int) Integer.parseInt(p2.getNumberOfRecords()) - Integer.parseInt(p1.getNumberOfRecords())
    

    So the simple compare of an integer in a String data type would not result correctly but to parse the string first by:

    Integer.parseInt(string)

    and get the true value of the number string.

    0 讨论(0)
  • 2020-12-21 14:19

    You can use the following code to sort your integer list is descending order.Here we are overriding compare() so that it sorts in descending order.

    //sort list in desc order 
                Collections.sort(myIntegerList, new Comparator<Integer>() {
                    public int compare(Integer one, Integer other) {
                         if (one >= other) {
                             return -1;
                         } else {
                             return 1;
                         } 
                       }
                });
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-21 14:24

    Try with this Comparator.

    Comparator objComparator = new Comparator() {
        public int compare(Object o1, Object o2) {
            int no1 = Integer.parseInt((String) o1);
            int no2 = Integer.parseInt((String) o2);
            return  no1 < no2 ? -1 : no1 == no2 ? 0 : 1;
        }
    };
    Collections.sort(myIntegerList, objComparator);
    
    0 讨论(0)
提交回复
热议问题