1

I am trying to import an excel document, add and delete some records and then sort the records by there columns before exporting it back to excel. In total there are 20 columns that are import/exported.

I created arraylists to capture the columns information. After processing them I am trying to sort them.

static List<String> rA_column = new ArrayList<String> ();
static List<String> rB_column = new ArrayList<String> ();
static List<String> rC_column = new ArrayList<String> ();

How can I sort them by rC_column first and then rA_column, but still maintain all the records together without accidentally mixing up cells causing the records information to be inaccurate?

I do not understand how I could use map or collect sort for this to work because its limiting me to two strings in map and I have 20 arraylists to keep in sync.

1
  • 1
    Why not represent a row as a Model and then use List<Model> and further sort that list based on fields you're talking about. Commented Sep 4, 2017 at 11:12

1 Answer 1

4

Don't create three different Lists. Instead you could create a class holding all three values and that you can sort by any fields which keeps the records together e.g.

class ExcelRow {
    String field1;
    String field2;
    String field3;
}

And you would sort it like this Sort ArrayList of custom Objects by property

Sign up to request clarification or add additional context in comments.

3 Comments

public static void Attribute_Insert (){ BufferedReader br = null; br = new BufferedReader(new FileReader(sample_dataset)); while ((line = br.readLine()) != null) { String[] attributes = line.split(delimiter); List<String> stringAttributes = new ArrayList<String>(); for(int i = 0; i<attributes.length; i++) {stringAttributes.add(attributes[i]);} OA_column = stringAttributes.get(0); OB_column = stringAttributes.get(1); OC_column = stringAttributes.get(2); rA_column.add(OA_column); rB_column.add(OB_column); rC_column.add(OC_column);} rectotal = count;}
I'm sorry @muratk.I am new to this environment and cannot get my previous comment to go into code block. Should I create a new class"ExcelRow" and import them in that class or use my original class to import? I don't understand how to reference between two classes after import without making a list?
@Brown You would have a List<ExcelRow> and pass the columns from every row into one new single object e.g. list.add(new ExcelRow(field1, field2, field3) and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.