Hugo February 2016

Checking order 2d array issue

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])        

Answers


Arnaud Broc February 2016

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.

Post Status

Asked in February 2016
Viewed 2,788 times
Voted 4
Answered 1 times

Search




Leave an answer