【发布时间】:2016-02-08 10:07:21
【问题描述】:
我有一个 XML 页面,其中有 4 列中的数据。这 4 列需要排序,首先在第 1 列,然后在第 2 列,依此类推。在前两列中有文本,第三列包含一个 3 位数字,最后一列包含一个双精度或大十进制。我从 XML 中获取数据并放入一个二维数组以检查排序是否正确。为了测试我为检查列中的顺序而编写的代码,我创建了一个小的二维数组: 字符串 [][] xml = 新字符串 [10][4];
回家后 182 5.1
回家后 182 5.9
回家后 182 5.10
在客场 150 4.6 之前
在犹他州 852 3.8 之前
在锡安 110 110.99 之前
在锡安 110 110.100 之前
控制无处不在 475 65.1
控制遥控器 369 38.6
控制 VoIP 598 44.9
这是我用来检查列顺序的整个代码部分(可以复制粘贴到 Eclipse):
String [][] xml = new String [10][4];
//column 1
xml[0][0]="After";
xml[1][0]="After";
xml[2][0]="After";
xml[3][0]="Before";
xml[4][0]="Before";
xml[5][0]="Before";
xml[6][0]="Before";
xml[7][0]="Control";
xml[8][0]="Control";
xml[9][0]="Control";
//column 2
xml[0][1]="Home";
xml[1][1]="Home";
xml[2][1]="Home";
xml[3][1]="Away";
xml[4][1]="Utah";
xml[5][1]="Zion";
xml[6][1]="Zion";
xml[7][1]="Everywhere";
xml[8][1]="Remote";
xml[9][1]="Voip";
//column 3
xml[0][2]="182";
xml[1][2]="182";
xml[2][2]="182";
xml[3][2]="150";
xml[4][2]="852";
xml[5][2]="110";
xml[6][2]="110";
xml[7][2]="475";
xml[8][2]="369";
xml[9][2]="598";
//column 4
xml[0][3]="5.1";
xml[1][3]="5.9";
xml[2][3]="5.10";
xml[3][3]="4.6";
xml[4][3]="3.8";
xml[5][3]="110.99";
xml[6][3]="110.100";
xml[7][3]="65.1";
xml[8][3]="38.6";
xml[9][3]="44.9";
for (int p=1;p<xml.length;p++){
int column1 = xml[(p)][0].compareTo(xml[(p-1)][0]);
boolean sortcolumn1 = column1>=0; //If value 0 then cells match (boolean True). if value equals 1 then value in second cell is further in the alphabet then the cell before it(boolean True). if value equals -1 then value in second cell is earlier in the alphabet then the cell before it(boolean False).
if(sortcolumn1==false){//Check on 1st column
System.out.println("1st Colum error: " +xml[(p)][0]+ " is before " +xml[(p-1)][0]);
}
//Checking the data in the 2nd column should only be done when the data in the two adjacent cells of the first column is equal.
if (column1 == 0) {
int column2 = xml[(p)][1].compareTo(xml[(p - 1)][1]);
if (column2 < 0) {// Check on 2nd column
System.out.println("Column2 error: " + xml[(p)][1] + " is before " + xml[(p - 1)][1]);
}
if (column2 == 0) {
if (xml[(p - 1)][2] != "" && xml[(p)][2] != "") {
try {
Integer a = Integer.parseInt(xml[(p)][2]);
Integer b = Integer.parseInt(xml[(p - 1)][2]);
Integer column3 = a -b;
if (column3 < 0) {// Check on 3rd column
System.out.println("Calculation column3: "+a+"-"+b+"=" + column3);
System.out.println("Column 3 error: " + xml[(p)][2] + " is lower then " + xml[(p - 1)][2]);
}
} catch (NumberFormatException e) {
}
int column3 = xml[(p)][2].compareTo(xml[(p - 1)][2]);
if (column3 == 0) {
if (xml[(p - 1)][3] != "" && xml[(p)][3] != "") {
try {
BigDecimal a = new BigDecimal(xml[(p)][3]);
BigDecimal b = new BigDecimal(xml[(p - 1)][3]);
BigDecimal column4 = a.subtract(b);
if (column4.compareTo(BigDecimal.ZERO) < 0) {// Check on 4th column
System.out.println("Calculation column4: " +a+"-"+b+"="+ column4);
System.out.println("Column 4 error: " + xml[(p)][3] + " is lower then " + xml[(p - 1)][3]);
}
} catch (NumberFormatException e) {
}
}
}
}
}
}
}
}
代码似乎工作正常,但我认为 5.9 低于 5.10 和 110.99 低于 110.100 的代码不同意。这是我得到的打印结果:
Calculation column4: 5.10-5.9=-0.80
Column 4 error: 5.10 is lower then 5.9
Calculation column4: 110.100-110.99=-0.890
Column 4 error: 110.100 is lower then 110.99
我尝试过 double 而不是 BigDecimal,但 5.10 只显示为 5.1。我认为这是问题所在,但我不知道如何解决。我想可以通过使用 .作为分隔符并对两列进行排序,但这会带来一系列问题。
【问题讨论】:
-
这听起来不是假的“5.9 低于 5.10 而 110.99 低于 110.100”吗?
-
怎么回事? 9比10低,99比100低不是吗?
-
不在这个数值比较中,: 0.9 > 0.1 or 0.9 > 0.10 , 这个是一样的
-
我了解,但我的客户认为 0.10 不是 0.1,而是比 0.9 更高的数字。有没有办法按照客户想要的方式对其进行排序?我意识到这可能在数字上不正确。
-
在这种情况下,您可能希望将它们作为字符串进行比较,以便进行字典比较。 stackoverflow.com/questions/4064633/string-comparison-in-java
标签: java arrays eclipse sorting