Possible Duplicate:
Sort a two dimensional array based on one column

I have the data:

"Something1" "TRUE"
"Something2" "FALSE"
"Something3" "FALSE"
"Something4" "TRUE"

That I then store in a multidimensional array:

String[][] myData = new String[data.length][2];

Now I want to sort this array so that it is ordered by "TRUE" first, so it becomes:

"Something1" "TRUE"
"Something4" "TRUE"
"Something2" "FALSE"
"Something3" "FALSE"

I'm looking at Arrays.sort(); but not sure how to implemtent this or if this is even the best way.

share|improve this question
1  
instead of having String[][] you should use YourClass[] where YourClass is a class you write yourself which contains the required information and implements Comparable<YourClass> – jlordo Dec 18 '12 at 12:39
feedback

closed as exact duplicate by jlordo, PearsonArtPhoto, Karthik T, C. Ross, scvalex Dec 18 '12 at 15:31

This question covers exactly the same content as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

3 Answers

up vote 2 down vote accepted

Sort your array with a custom comparator:

Arrays.sort(myData , new Comparator<String[]>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                return ((String) o2[1]).compareTo(o1[1]);
            }
        });
share|improve this answer
Thanks it now sorts, however sorts false to true, any idea how to sort reverse? – ubergam3r Dec 18 '12 at 16:04
@ubergam3r you can use return ((String) o1[1]).compareTo(o2[1]); – Del Shekasteh Dec 18 '12 at 16:16
of course stupid me...... sorry brains a bit fried. thanks for the help – ubergam3r Dec 18 '12 at 16:17
feedback

If you have to use an array, I would use Arrays.sort() with a custom comparator. That comparator would sort on the appropriate element of the String[] passed in.

I would perhaps not use a multidimensional array, but rather implement some object for each row of your array. That data looks tightly tied together, rather than just being elements in an array. You can type it appropriately - at the moment you're storing a boolean as a string.

share|improve this answer
feedback

OK here you are. Two self-contained example solutions you can run from a test main class. The first one using multi-dimensional array.

    Object values[][] = new Object[][] {
            { "a", Boolean.FALSE }, 
            { "b", Boolean.TRUE }, 
            { "c", Boolean.TRUE },              
    };

    Arrays.sort(values, 
            new Comparator<Object[]>() {
                @Override
                public int compare(Object[] tuple1, Object[] tuple2) {
                    int result = -1*((Boolean) tuple1[1]).compareTo((Boolean) tuple2[1]);
                    if (result == 0) {
                        result = ((String) tuple1[0]).compareTo(((String) tuple2[0]));
                    }                       
                    return result;
                }
            }
    );

    for (Object[] tuple : values) {
        System.out.println(Arrays.toString(tuple));
    }

The second one using a generic (and type-safer) Tuple.

    class Tuple<A, B> {
        private final A first;
        private final B second;

        public Tuple(A first, B second) {
            this.first = first;
            this.second = second;
        }

        public A getFirst() {
            return this.first;
        }

        public B getSecond() {
            return this.second;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String toString() {
            return "[first=" + first.toString() + ",second="  + second.toString() + "]";
        }
    };

    Tuple<String, Boolean> values[] = new Tuple[] {
            new Tuple<String, Boolean>("a", Boolean.FALSE), 
            new Tuple<String, Boolean>("b", Boolean.TRUE),
            new Tuple<String, Boolean>("c", Boolean.TRUE),              
    };

    Arrays.sort(values, 
            new Comparator<Tuple<String, Boolean>>() {
                @Override
                public int compare(Tuple<String, Boolean> tuple1, Tuple<String, Boolean> tuple2) {
                    int result = -1*tuple1.getSecond().compareTo(tuple2.getSecond());
                    if (result == 0) {
                        result = tuple1.getFirst().compareTo(tuple2.getFirst());
                    }                       
                    return result;
                }
            }
    );

    for (Tuple<String, Boolean> tuple : values) {
        System.out.println(tuple);
    }
share|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.