排序 ArrayList<Object>按可比接口 [英] Sorting ArrayList&lt;Object&gt; by Comparable Interface

查看:28
本文介绍了排序 ArrayList<Object>按可比接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想对对象的数组列表进行排序.为此,我正在使用 Comparable 接口.它完全有效但问题是我在排序时出现了这两个问题.

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. 所有第一个字母大写的名字都放在顶部,所有第一个字母小写的名字都放在底部.

  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.

所有排序后的大写字母单词都放在一起,然后所有小写字母单词放在一起.

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

这是我的豆子

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

    }

这是输出

我认为它是基于 ASCII 码的.我想要一个完整的排序列表.请看一下.

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

推荐答案

如果您希望进行不区分大小写的排序,我建议您更改您的 compareTo() 方法以改为使用 String 的 >compareToIgnoreCase() 方法 - 即:

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

这篇关于排序 ArrayList<Object>按可比接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆