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.

Brown
  • 11
  • 1
  • 1
    Why not represent a row as a `Model` and then use `List` and further sort that list based on fields you're talking about. – Naman Sep 04 '17 at 11:12

1 Answers1

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

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • 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 stringAttributes = new ArrayList(); for(int i = 0; i – Brown Sep 04 '17 at 12:05
  • 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 Sep 04 '17 at 12:10
  • @Brown You would have a `List` and pass the columns from every row into one new single object e.g. `list.add(new ExcelRow(field1, field2, field3)` and so on. – Murat Karagöz Sep 04 '17 at 12:12