简体   繁体   中英

Sorting ArrayList<Object> by Comparable Interface

Actually I want to sort Array List of objects. I am using Comparable interface for this purpose. It is completely working But the problem is that when I am sorting is is giving these two problems.

  1. All name who have 1st letter capital are coming together on the top and all name who have 1st letter small are coming together on the bottom.

  2. All the sorted Capital letters word are coming together after that all the small letter words are coming at bottom together.

Here is my bean

MyShares.java

   public class Myshares implements Comparable<Myshares> {

        int id, parent;
        String name, path, type, shared_with, shared_or_not, upload_request;

        public int getParent() {
            return parent;
        }

        public void setParent(int parent) {
            this.parent = parent;
        }

        public String getUpload_request() {
            return upload_request;
        }

        public void setUpload_request(String upload_request) {
            this.upload_request = upload_request;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getShared_with() {
            return shared_with;
        }

        public void setShared_with(String shared_with) {
            this.shared_with = shared_with;
        }

        public String getShared_or_not() {
            return shared_or_not;
        }

        public void setShared_or_not(String shared_or_not) {
            this.shared_or_not = shared_or_not;
        }

        @Override
        public int compareTo(Myshares another) {

            return this.name.compareTo(another.getName());
        }

    }

This is the output

在此处输入图片说明

I think it is based on ASCII code. I want a complete sorted list. Please take a look.

If you are wishing for a case-insensitive sort, I would recommend changing your compareTo() method to instead use the compareToIgnoreCase() method of String - namely:

public int compareTo(Myshares another) {
    return this.name.compareToIgnoreCase(another.getName());
}

Change

@Override
public int compareTo(Myshares another) {
    return this.name.compareTo(another.getName());
}

to

@Override
public int compareTo(Myshares another) {
    return String.CASE_INSENSITIVE_ORDER.compare(this.name, another.getName());
}

or use the more readable and also very good solution posted by nullPainter .

你应该调整你的compareTo()方法

return this.name.toLowerCase().compareTo(another.getName().toLowerCase());

Replace the compareTo method with:

public int compareTo(Myshares another) {
  return this.name.compareToIgnoreCase(another.getName());
}

I gave you some source to an existing Comparator I wrote which does full Alpha Numeric sorting and will work with numbers in the names like 10, 1, etc

To use it you can do: Collections.sort(myList, comparator);

An alternative would also be in your compare method to lowercase both sides. But anytime you have numbers or symbols it will be thrown off so I would personally use the Comparator.

Checkout the GIST with the source code at: https://gist.github.com/gondor/9328de5fa0cce130bc3b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM