Android java custom sorting using Comparable

后端 未结 3 1135
-上瘾入骨i
-上瘾入骨i 2021-01-20 11:11
  @Override
      public int compareTo(final myRow another) {

        final int BEFORE    =-1;
        final int EQUAL     = 0;
        final int AFTER     = 1;

           


        
相关标签:
3条回答
  • 2021-01-20 11:37

    Your code looks a bit complex. The simplest way would be to start from the highest priority field and move down to other fields. An example code would be:

    public class Member implements Comparable<Member> {
    
        static enum Status {
            NORMAL(1), FRIEND(2), BANNED(3);
    
            private final int order;
    
            Status(int order) {
                this.order = order;
            }
    
            public int getOrder() {
                return this.order;
            }
    
        };
    
        private final String name;
    
        private final Status status;
    
        public Member(final String name, final Status status) {
            this.name = name;
            this.status = status;
        }
    
        @Override
        public int compareTo(Member o) {
            if (this.status.equals(o.status)) {
                return this.name.compareTo(o.name);
            } else {
                return this.status.compareTo(o.status);
            }
        }
    
        @Override
        public String toString() {
            return "Member [name=" + name + ", status=" + status + "]";
        }
    
        public static void main(String[] args) throws Throwable {
            Member[] members = {
                            new Member("abrt", Status.FRIEND),
                            new Member("dfgh", Status.FRIEND),
                            new Member("abdf", Status.NORMAL),
                            new Member("wert", Status.NORMAL),
                            new Member("andgh", Status.BANNED),
                            new Member("qwer", Status.BANNED)
            };
            List<Member> lst = Arrays.asList(members);
            Collections.sort(lst);
            System.out.println(lst);
        }
    
    
    }
    
    0 讨论(0)
  • 2021-01-20 11:53

    Your ABC sort logic doesn't work. If you pass in two USER_TYPE_FRIEND objects for instance, whatever their respective order, compareTo will always return BEFORE.

    You need to implement this by first comparing the usertype.

    If they are equal, you can return your row.compareTo(...) expression.

    If not, you need to return before/after depending only on how those types "compare" in your logic (i.e. friend < normal < banned).

    0 讨论(0)
  • 2021-01-20 11:54

    my code now :

      public enum user_type {USER_TYPE_FRIEND(1), USER_TYPE_NORMAL(2), USER_TYPE_BANNED(3);
          private final int order;
          user_type(int order) {
              this.order = order;
          }
          public int getOrder() {
              return this.order;
          }
      }
    

    ...

      @Override
      public int compareTo(final myRow another) {
    
        if (sorttype==sort_type.SORT_ABC) {
            if (this.getUserType().equals(another.getUserType())) {
                return this.getRow().toLowerCase().compareTo(another.getRow().toLowerCase());
            } else {
                return this.getUserType().compareTo(another.getUserType());
            }
        } 
        else {
            //LOGINTIME
            return this.getUserType().compareTo(another.getUserType());
        }
      }
    

    Thanks, Sanjay!

    0 讨论(0)
提交回复
热议问题