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 7777009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:01:41+00:00 2026-06-01T18:01:41+00:00

i have an array list of type String[]. i want to order it by

  • 0

i have an array list of type String[]. i want to order it by String[0] as a int. i have this:

Collections.sort(listitems, new Comparator<String[]>() {
 @Override
 public int compare(String[] lhs, String[] rhs) {
  return lhs[0].compareToIgnoreCase(rhs[0]);
 }
});

but this order like this:
10
11
12
2
20
21
3
4
5
i have tried to convert the lhs[0] and rhs[0] to int, but the int type doesn’t have any kind of compare and i’m not sure what type of int i need to return

  • 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-06-01T18:01:43+00:00Added an answer on June 1, 2026 at 6:01 pm

    The result from compare is meant to be:

    • Negative if the first argument is logically “less than” the second
    • Positive if the first argument is logically “more than” the second
    • Zero if the first argument is logically equal to the second

    So if you’re sure that the first string of each array is parsable as an integer, I’d use:

    @Override
    public int compare(String[] lhs, String[] rhs) {
        int leftInt = Integer.parseInt(lhs[0]);
        int rightInt = Integer.parseInt(rhs[0]);
        return leftInt < rightInt ? -1
            : leftInt > rightInt ? 1
            : 0;
    }
    

    Java 1.7 has the helpful Integer.compare method, but I assume that won’t be available to you. You could use Integer.compareTo but that may create more garbage than you really want on a mobile device…

    Note that this will work even when leftInt and rightInt are very large, or possibly negative – the solution of just subtracting one value from another assumes that the subtraction won’t overflow.

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

Sidebar

Related Questions

I have an array containing a list of files. I want to sort it
Hi all I have list of type string with some items. i want to
I have an array of type string which I want to re-use inside a
I have five lists List<String> . I want to get a single array which
I have an array list of objects in my application. private static ArrayList<Player> userList=new
I have an array list of objects and I am using this example to
Closed as exact duplicate of this question . I have an array/list of elements.
I have a collection (or list or array list) in which I want to
I have an array which is a list of domains, I want to print
I have an ArrayList of type String that contain certain values. I want to

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.