Java 排序数组列表<;对象>;通过可比界面
实际上,我想对对象的数组列表进行排序。为此,我使用可比较的接口。这是完全可行的,但问题是,当我排序时,会给出这两个问题Java 排序数组列表<;对象>;通过可比界面,java,sorting,arraylist,comparable,Java,Sorting,Arraylist,Comparable,实际上,我想对对象的数组列表进行排序。为此,我使用可比较的接口。这是完全可行的,但问题是,当我排序时,会给出这两个问题 所有第一个字母大写的名字都在顶部,所有第一个字母小写的名字都在底部 所有排序的大写字母单词都在一起,然后所有小写字母单词都在底部一起 这是我的豆子 MyShares.java public class Myshares implements Comparable<Myshares> { int id, parent; Stri
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());
}
}
公共类Myshares实现了可比较的{
int-id,父;
字符串名称、路径、类型、共享对象、共享对象或非共享对象、上载请求;
public int getParent(){
返回父母;
}
公共void setParent(int parent){
this.parent=parent;
}
公共字符串getUpload_请求(){
返回上传请求;
}
公共无效设置加载请求(字符串上载请求){
this.upload\u request=上传\u请求;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getPath(){
返回路径;
}
公共void setPath(字符串路径){
this.path=path;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共字符串getShared_with(){
返回与共享的_;
}
public void setShared_with(字符串shared_with){
this.shared_with=shared_with;
}
公共字符串getShared_或_not(){
返回共享_或不返回共享_;
}
公共无效设置共享或非共享(字符串共享或非共享){
this.shared_或_not=shared_或_not;
}
@凌驾
公共int比较(我共享另一个){
返回这个.name.compareTo(另一个.getName());
}
}
这是输出
我认为它是基于ASCII码的。我要一份完整的分类清单。请看一看。如果您希望进行不区分大小写的排序,我建议您将
compareTo()
方法改为使用String
的compareTignoreCase()
方法,即:
public int compareTo(Myshares another) {
return this.name.compareToIgnoreCase(another.getName());
}
改变
到
或者使用由发布的更具可读性且非常好的解决方案。您应该调整
compareTo()
方法
return this.name.toLowerCase().compareTo(another.getName().toLowerCase());
将compareTo方法替换为:
public int compareTo(Myshares another) {
return this.name.compareToIgnoreCase(another.getName());
}
我给了你一些我写的现有比较器的源代码,该比较器进行完整的字母数字排序,并将处理名称中的数字,如10、1等 要使用它,您可以执行以下操作:Collections.sort(myList,comparator) 另一种方法是在compare方法中使用小写两边。但任何时候你有数字或符号,它都会被扔掉,所以我个人会使用比较器
使用源代码检查要点:如果您认为这是基于ASCII代码的,为什么不比较一下.toUpper()?我必须承认,nullPainter提出的
compareToIgnoreCase
将是更好的解决方案,因为我的不是null safet,您不认为它很长吗?根据您的需要,如果您使用1name、2name、10name或任何其他符号来点击字符串,您会发现它将像1name一样排序,10name等。此比较器考虑了所有这些因素,并进行了完美排序。甚至会对IP地址或主机名进行正确排序。在这个网站上,仅链接的答案不太好。如果链接变得过时,那么答案是无用的。您应该始终复制答案的相关部分,并且仍然可以链接到源代码。不幸的是,许多应用程序中缺少智能处理数字分量的比较器,因此至少+1@jlordo:可能是因为输入错误(compareTognoringCase()
,而不是compareTognorecase()
)。观众很难接受!我一看到它就把它修好了;)@谢谢你,先生,你的解决方案对我有用+谢谢你的回答。
return this.name.toLowerCase().compareTo(another.getName().toLowerCase());
public int compareTo(Myshares another) {
return this.name.compareToIgnoreCase(another.getName());
}