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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:33:25+00:00 2026-05-13T23:33:25+00:00

I have an arraylist set up. I have input instuctions set up too, so

  • 0

I have an arraylist set up. I have input instuctions set up too, so that the user can enter one string, then one integer, then one string (the first name, the age, and the last name).

I need to sort the arraylist by the last name. The code I have entered so far is all under the main method:-

public static void main(String[] args) {
Name Name[] = new Name[50];
int count = 0;

for (int i=0; i<50; i++)
  NewName[i] = new Name();

//ADD NEW TO ARRAYLIST NAME
String FName = JOptionPane.showInputDialog("first name");
int age = Integer.parseInt(JOptionPane.showInputDialog("age"));
String LName = JOptionPane.showInputDialog("last name");
          NewName[count] = new Name(FName, age, LName);
count = count++;
}

//ITEMS SORT BY LAST NAME
//CODE FOR SORT GOES HERE
  • 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-13T23:33:25+00:00Added an answer on May 13, 2026 at 11:33 pm

    Take a look at Comparable, Comparator, Arrays.sort and Collections.sort

    import java.util.Arrays;
    
    
    class Name implements Comparable<Name> {
    
        private String lastName;
        //Other fields
    
        public Name(String lastName){
            this.lastName = lastName;
        }
    
        public int compareTo(Name o) {
            //Null checks etc
            return lastName.compareTo(o.lastName);
        }
        public String getLastName(){
            return lastName;
        }
        //Getter and setter methods
    }
    
    public class Test{
        public static void main(String[] args) {
            Name[] arr = new Name[]{new Name("AAC"), new Name("AAD"), new Name("AAA"),new Name("ABC"), new Name("AADDE")};
            Arrays.sort(arr);
            for(Name nm:arr){
                System.out.println(nm.getLastName());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a EditText box so a user can input names and then click
I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have the following code: ArrayList<Integer> arr = new ArrayList<Integer>(10); arr.set(0,5); I am getting
I have an ArrayList that is set to Friend. Once I click my button
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have an arraylist that gets different type of values in it, 1st value->
I have an arraylist that holds a subset of names found in my database.
I have an ArrayList that im populating within java and passing it off to
I have an ArrayList which is set as an attribute of the response object
I have an ArrayList of objects that I need to sort in two different

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.