0

I have a XML page where there is data in 4 columns. These 4 columns need to be sorted, first on the 1st column, then on the 2nd column and so on. In the first two columns there is text, the third column contains a 3digit number and the final column contains a double or bigdecimal. I take the data from the XML and put in a a 2d array to check if the ordering is correct. In order to test the code I've written to check the ordering in the columns I've created a small 2d array: String [][] xml = new String [10][4];

After Home 182 5.1

After Home 182 5.9

After Home 182 5.10

Before Away 150 4.6

Before Utah 852 3.8

Before Zion 110 110.99

Before Zion 110 110.100

Control Everywhere 475 65.1

Control Remote 369 38.6

Control Voip 598 44.9

This is the entire section of code I used to check the ordering of the columns (can be copy pasted to 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) {
                                }
                            }
                        }
                    }
                }
            }
        }
    }

The code seems to work fine but where I think 5.9 is lower then 5.10 and 110.99 is lower then 110.100 the code disagrees. This is the print result I get:

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

Ive tried double instead of BigDecimal but then the 5.10 just shows as 5.1. I think this is the problem but I can't figure out how to solve it. I guess it would be possible by splitting the final column in two using the . as a seperator and the sorting the two column but this would bring it's own set of problems.

Hugo
  • 313
  • 2
  • 6
  • 19
  • Doesn't this sound false "5.9 is lower then 5.10 and 110.99 is lower then 110.100 " ? – Arnaud Feb 08 '16 at 10:10
  • how so? 9 is lower then 10 and 99 is lower then 100 isn't it? – Hugo Feb 08 '16 at 10:11
  • Not in this numerical comparison, : 0.9 > 0.1 or 0.9 > 0.10 , this is the same – Arnaud Feb 08 '16 at 10:12
  • I understand but my customer does not see 0.10 as 0.1 but as a higher number then 0.9. Is there a way to sort it the way the customer wants to? I realize this may not be numerically correct. – Hugo Feb 08 '16 at 10:20
  • In this case, you may want to compare them as strings, so that you get a lexicographical comparison. http://stackoverflow.com/questions/4064633/string-comparison-in-java – Arnaud Feb 08 '16 at 10:27

1 Answers1

0

You can try to use POJO instead of 2d array.

public class Test implements Comparable<Test> {

private String field1;

private String field2;

private Integer field3;

private Double field4;

@Override
public int compareTo(Test o) {
    // There is your sorting algorythm
    return 0;
}

Then after handle the data in a list of Test class, u just sort all with :

    final List<Test> testList = new ArrayList<Test>();
    Collections.sort(testList);

Hope that help.