Java中的排序对象列表-包含十进制数的字符串对象

Java中的排序对象列表-包含十进制数的字符串对象,java,arrays,Java,Arrays,我有3.1-3.20之间的数字。这是一组问题编号,如下所示 List<Line> liner = new ArrayList<>(); for(int counter = 0; counter < rpkKode.size(); counter++) { Line linerObj = new Line(); linerObj.setQuestionNmbr(rpkKode.get(counter).getQuestionNumber());

我有3.1-3.20之间的数字。这是一组问题编号,如下所示

List<Line> liner = new ArrayList<>();
for(int counter = 0; counter < rpkKode.size(); counter++) {
    Line linerObj = new Line();
    linerObj.setQuestionNmbr(rpkKode.get(counter).getQuestionNumber());
    liner.add(linerObj);
}
List liner=new ArrayList();
对于(int counter=0;counter
当我打印时,它打印如下: 3.1, 3.11, 3.12, ... 3.19,3.2,3.20,3.3,3.4

我想按顺序打印。我是否可以执行任何操作(可能使用第三方),以便按顺序将对象排序为3.1到3.20


我无法将其转换为Double,因为我需要使用JSTL在屏幕上显示它,它是get问题,同时处理1.10、1.20或1.200以及许多与验证相关的Jquery问题。

您可以使用Collections.sort(对象、比较器)
如果默认比较器不适用于您,您可以创建自定义比较器,以便按照您想要的任何顺序和规则对对象进行排序。

您可以使用自己的比较器,并使用小数点后的数字比较问题编号。

您可以在
类中实现可比接口:

public class Line implements Comparable<Line> {

    ...

    @Override
    public int compareTo(Line o){
        // if question number is type of Integer
        return Integer.compare(this.getQuestionNumber(), o.getQuestionNumber());
    }
}
public类行实现可比较的{
...
@凌驾
公共整数比较(o行){
//如果问题编号是整数类型
返回整数.compare(this.getQuestionNumber(),o.getQuestionNumber());
}
}

然后,您可以使用
Collections.sort(liner)

如果处理的是唯一字符串,也可以使用TreeSet

<强>让我们考虑下面的行类:< /强>

public class Line implements Comparable<Line> {

    String questionNo;

    public Line(String questionNo) {
        super();
        this.questionNo = questionNo;
    }

    public String getQuestionNo() {
        return questionNo;
    }
    public void setQuestionNo(String questionNo) {
        this.questionNo = questionNo;
    }

    @Override
    public int compareTo(Line obj){

        String splitted1[] = this.getQuestionNo().split("\\.");
        String splitted2[] = obj.getQuestionNo().split("\\.");

        if(Integer.parseInt(splitted2[0]) > Integer.parseInt(splitted1[0])) {
            return -1;
        } 

        if(splitted1.length == 0) {
            return -1;
        }

        if(splitted2.length == 0) {
            return 1;
        }

        return this.getQuestionNo().compareTo(obj.getQuestionNo());
    }
}
public类行实现可比较的{
字符串问号;
公用线路(字符串编号){
超级();
this.questionNo=questionNo;
}
公共字符串getQuestionNo(){
返回问题编号;
}
public void setQuestionNo(字符串questionNo){
this.questionNo=questionNo;
}
@凌驾
公共整数比较(行obj){
String splitted1[]=this.getQuestionNo().split(“\\”);
字符串splitted2[]=obj.getQuestionNo().split(“\\”);
if(Integer.parseInt(splitted2[0])>Integer.parseInt(splitted1[0])){
返回-1;
} 
如果(拆分1.length==0){
返回-1;
}
如果(拆分2.length==0){
返回1;
}
返回这个.getQuestionNo().compareTo(obj.getQuestionNo());
}
}
现在试试看

Set<Line> ts = new TreeSet<Line>();
ts.add(new Line("3.1"));
ts.add(new Line("3.11"));
ts.add(new Line("3.12"));
ts.add(new Line("3.19"));
ts.add(new Line("3.2"));
ts.add(new Line("3"));
ts.add(new Line("3.20"));
ts.add(new Line("3.3"));
ts.add(new Line("3.4"));

for (Line line : ts) {
    System.out.println(line.getQuestionNo());
}
Set ts=new TreeSet();
ts.add(新行(“3.1”);
ts.add(新行(“3.11”);
ts.add(新行(“3.12”);
ts.add(新行(“3.19”);
ts.add(新行(“3.2”);
ts.add(新行(“3”);
ts.add(新行(“3.20”);
ts.add(新行(“3.3”);
ts.add(新行(“3.4”);
用于(行:ts){
System.out.println(line.getQuestionNo());
}

我做得很匆忙。如果您发现任何问题,请告诉我。

我使用了NaturalOrderComparator.java——在java中对字符串执行“自然顺序”比较

Pierre Luc Paour 2003年版权所有的文件(C)。链接()

以下代码将按预期对数字进行排序

Map<String, Class> questionsMap = new TreeMap<>(new NaturalNumberComparator());
Map questionsMap=newtreemap(newnaturalnumbercomparator());

如果有更好的解决方案,请分享。

您可以使用
列表#排序
功能编写您自己的竞争对手,或者如果有
[3.20,4.10,3.30]
?您可以让比较器先检查整数部分,然后再检查小数部分。这将给出3.11,3.12,3.19,3.2,3.20,3.3,3.4,我第一次把你的问题弄错了。请看我的更新答案。更新给出:3.1,3.11,3.12,3.19,3.2,3.20,3.3,3.4。不,这不是解决办法