Search code examples
javasortingobjectarraylistlocaldate

Sorting 2D Arraylist of Object


I have 2D Arralist of Object and i want to sort it upon first column which contains LocalDate

i used this code to sort it

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
    formatter = formatter.withLocale( Locale.US );

ArrayList<ArrayList<Object>> cusKashf = new ArrayList<ArrayList<Object>>();

cusKashf.addAll(new ReadBillDeateals().readSell(textField_4.getText()));
Collections.sort(cusKashf , new Comparator<ArrayList<Object>>() {
      @Override
      public int compare(ArrayList<Object> entry1, ArrayList<Object> entry2) {
             return ((LocalDate.parse(entry1.get(0).toString(), formatter)).compareTo((LocalDate.parse(entry2.get(0).toString(), formatter))));
            }
        });

the problem is this code make an exception, i wonder where's the mistake in me code

this is the exception report

java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at reports.Kashf$7$1.compare(Kashf.java:327)
    at reports.Kashf$7$1.compare(Kashf.java:1)
    at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at reports.Kashf$7.actionPerformed(Kashf.java:324)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)

Solution

  • In your cusKashf List check if the sublist is not empty.

    @Override
      public int compare(ArrayList<Object> entry1, ArrayList<Object> entry2) {
         if(Objects.isNull(entry1) || entry1.isEmpty())  return -1;
         if(Objects.isNull(entry2) || entry2.isEmpty())  return -1;
    
         return ((LocalDate.parse(entry1.get(0).toString(), formatter)).compareTo((LocalDate.parse(entry2.get(0).toString(), formatter))));
      }