0

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.

2
  • 1
    stackoverflow.com/questions/4907683/… Commented Dec 18, 2012 at 12:39
  • 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> Commented Dec 18, 2012 at 12:39

3 Answers 3

2

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]);
            }
        });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it now sorts, however sorts false to true, any idea how to sort reverse?
@ubergam3r you can use return ((String) o1[1]).compareTo(o2[1]);
of course stupid me...... sorry brains a bit fried. thanks for the help
0

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.

Comments

0

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);
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.