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.

Community
  • 1
  • 1
ubergam3r
  • 189
  • 1
  • 3
  • 11
  • 1
    http://stackoverflow.com/questions/4907683/sort-a-two-dimensional-array-based-on-one-column – dinesh707 Dec 18 '12 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` – jlordo Dec 18 '12 at 12:39

3 Answers3

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]);
            }
        });
Juvanis
  • 25,802
  • 5
  • 69
  • 87
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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
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);
    }
SkyWalker
  • 13,729
  • 18
  • 91
  • 187