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
8
  • 1
    There is convention to name variables from small letter in java. Commented Mar 13, 2010 at 17:40
  • You are using an array of Name objects, not actually using an ArrayList. Commented Mar 13, 2010 at 17:41
  • Sorting depends on the implementation of the Name object. I can only hint you about Arrays.sort, Collections.sort, and Comparator. Commented Mar 13, 2010 at 17:43
  • just as a side note: count = count++; will increase count by... 0, zero, nil, nought. count = ++count; would increase count by 1. count++; would increase count by 1. count += 1; would increase count by 1. Commented Mar 13, 2010 at 17:47
  • So this is not an arraylist? It's just an array? I have tried to use:- Arrays.sort(NewName); Collections.sort(NewName); And both do not work, the compiler fails... Commented Mar 13, 2010 at 17:49

2 Answers 2

5

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());
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The null checks are actually not required here (or when instanceof is used).
@Helper Method Are you referring to the compareTo method? Actually it is, the parameter could be null.
@sauguta Sry, you're right, I thought String's compareTo() method would do the null check.
4

This isn't meant to be offensive but I would suggest you learn the basics before you move on to Swing.

1 Comment

I think Helper is suggesting arrays are the basics. I'd tend to agree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.