Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:34:36+00:00 2026-05-31T19:34:36+00:00

Ok, so I just now figured out how to sort a 2d array via

  • 0

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, I.E.

[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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:34:37+00:00Added an answer on May 31, 2026 at 7:34 pm

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working in Delphi7 just now, I noticed that not only a VarIsEmpty function exists,
I've done some Python but have just now starting to use Ruby I could
just trying to sort out a small delimma I'm having here. Currently, I'm working
I'm writing a merge sort function, and right now I am just using a
Using WSS 3.0 I have recently figured out how to aggregate the announcements from
I have an array that stores the order that I want to sort a
For a while now i have been trying to figure out the algorithms behind
Just now I needed to do something like the following query, and was surprised
Just now, I'm reading Josuttis' STL book. As far as I know -- c++
Just now find it by chance, Add(T) is defined in ICollection<T> , instead of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.