Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know the question was asked long ago but actually i had the same problem. If you dont know how many variables you would have on list but this is not a big number you could just implement comparator for every choose. eg</p> <p>I have <code>ArrayList&lt;ArrayList&lt;Object&gt;&gt;</code> and want to sort it by column's and i know that the nested list consist of variable number of objects i can just implement comparator for every possible value:</p> <pre><code>public class SecondColumnComparator implements Comparator { public static boolean isNumeric(String str) { try { Integer integer = Integer.parseInt(str); } catch (NumberFormatException nfe) { return false; } return true; } @Override public int compare(Object o1, Object o2) { if (isNumeric(((ArrayList&lt;String&gt;) o1).get(1))) { Integer firstInteger = Integer.parseInt(((ArrayList&lt;String&gt;) o1).get(1)); Integer secondInteger = Integer.parseInt(((ArrayList&lt;String&gt;) o2).get(1)); return firstInteger.compareTo(secondInteger); } if (((ArrayList&lt;Object&gt;) o1).get(1) instanceof String) { String firstString = ((ArrayList&lt;String&gt;) o1).get(1); String secondString = ((ArrayList&lt;String&gt;) o2).get(1); return firstString.compareTo(secondString); } throw new Exception(); } </code></pre> <p>}</p> <p>And call this this way:</p> <pre><code> switch (valueSelected) { case 0: Collections.sort(this.listOfLists, new FirstColumnComparator()); break; case 1: Collections.sort(this.listOfLists, new SecondColumnComparator()); break; case 2: Collections.sort(this.listOfLists, new ThirdColumnComparator()); break; case 3: Collections.sort(this.listOfLists, new FourthColumnComparator()); break; default: } </code></pre> <p>In every comparator just modifying <code>.get(x)</code> where x is number of collumn by which you want sort.</p> <p>The <code>boolean isNumeric(String str);</code> function may be used because you cant store different type of objects on one list so I put the recognition of this to the comparator and parse String to any other type.</p> <p>Remember that this <code>comparator</code> and its "calculations" are called to every single comparison made by algorithm so it is extremely inefficient... Despite this fact this is kind of sollution. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload