排序的ArrayList<对象>按可比接口 [英] Sorting ArrayList<Object> by Comparable Interface

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

问题描述

其实我想排序的对象数组列表。我使用可比接口用于这一目的。它完全工作,但问题是,当我整理的是给这两个问题。

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 code。我希望有一个完整的排序列表。请看一看。

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

推荐答案

如果你是希望一个不区分大小写的排序,我会建议改变你的的compareTo()方法改用字符串与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&LT;对象&gt;按可比接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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