简体   繁体   中英

CompareTo/Sorting 2D array of Strings AND Integers

Ok, so I just now figured out how to sort a 2d array via string and integers, however I am not sure how to do combine them.

I have a 2d array of users, each row has one users data. The data is string or integer respectively down the column so if I have multiple compare methods, such as sort by name or sort by phone number, how can I implement the initial 2d array since if I declare it as a String I can no longer compare by integer, and if i declare it as an integer I can no longer compare via String.

I am using the basic 2d sort method as of now.

    final Integer[][] data1 = userlistcopy;
    Arrays.sort(data1, new Comparator<Integer[]>() {

        @Override
        public int compare(final Integer[] entry1, final Integer[] entry2) {
            final Integer time1 = entry1[1];
            final Integer time2 = entry2[1];
            return time2.compareTo(time1);
        }
    });
    System.out.println("====");
    for (final Integer[] s : data1) {
        System.out.println(s[0] + " " + s[1]);
    }

or

final String[][] data1 = userlistcopy;
        Arrays.sort(data1, new Comparator<String[]>() {

            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[3];
                final String time2 = entry2[3];
                return time2.compareTo(time1);
            }
        });
        System.out.println("====");
        for (final String[] s : data1) {
            System.out.println(s[0] + " " + s[1]);
        }

So I is there any way to cast the variables if I am comparing a different variable type without making the compiler dislike me? Or is it possible for me to just declare the whole compareTo method an Integer (even with Strings involved) when I am only comparing the integer columns of the array?

Thanks!

EDIT, more detail: I have a 2D array, IE

[0][bob] [3][joe] [4][john] [6][jimmy]

It is stored in a String[][] userlistcopy; If I want to compareTo with String, it will work since I can compare userlistcopy[1] and ints can be seen as strings even though they are ignored. However, if I want to compare via Integer, I have to change userlistcopy to an Integer array, and then it freaks out since they're are Strings present, and i ignore the error it nulls out the String data slots.

EDIT (FIGURED IT OUT).

Ok, I figured it out! I just transferred all the data into an Object array, as I did so I declared their respective String/Int types, then I compared to via Object, and during the actual comparison I just casted/parsed it to my own needs, such as..

Arrays.sort(data1, new Comparator<Object[]>() {

        @Override
        public int compare(final Object[] entry1, final Object[] entry2) {
            final Integer time1 = Integer.parseInt(entry1[1].toString());
            final Integer time2 = Integer.parseInt(entry2[1].toString());
            return time2.compareTo(time1);
        }
    });

or

final Object[][] data1 = datanew;
    Arrays.sort(data1, new Comparator<Object[]>() {

        @Override
        public int compare(final Object[] entry1, final Object[] entry2) {
            final String time1 = entry1[2].toString();
            final String time2 = entry2[2].toString();
            return time2.compareTo(time1);
        }
    });

Thanks for the help though!

Fairly sure you do not need/want a 2D array. You want an 1D array of type 'User'

class User{
    public int ID;
    public String name;
}

Then make use of the custom comparator like you have already sorted for you arrays.

Integer solution:

Arrays.sort(data1, new Comparator<Object[]>() {

            @Override
            public int compare(final Object[] entry1, final Object[] entry2) {
                final Integer time1 = Integer.parseInt(entry1[1].toString());
                final Integer time2 = Integer.parseInt(entry2[1].toString());
                return time2.compareTo(time1);
            }
        });

or String

final Object[][] data1 = datanew;
    Arrays.sort(data1, new Comparator<Object[]>() {

        @Override
        public int compare(final Object[] entry1, final Object[] entry2) {
            final String time1 = entry1[2].toString();
            final String time2 = entry2[2].toString();
            return time2.compareTo(time1);
        }
    });

or Date

public int compare(final Object[] entry1, final Object[] entry2) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
                time1 = sdf.parse(entry1[3].toString());
                time2 = sdf.parse(entry2[3].toString());

                return time2.compareTo(time1);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }

        }
    });

I can compare them separately by making the whole 2d array an Object, then comparing them separately within the Object compareTO method. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM