Sorting ArrayList<Object> by Comparable Inte

2019-08-27 21:40发布

问题:

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.

回答1:

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());
}


回答2:

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.



回答3:

you should adjust your compareTo() method

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


回答4:

Replace the compareTo method with:

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


回答5:

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