Paul
Paul

Reputation: 289

Bubblesorting Object Array in Java

I'm attempting to create a method to bubblesort an array of objects to be displayed to the user in alphabetical order based on the name field assigned to the object. I've been playing around with trying to figure this out for a few days now and for the life of me I can't do it. All the research I've done hasn't really helped, tried a bunch of different code and they all seem to just stop the program/give me more issues. So I figured I'd ask. This is what I'm working with now.

public static void sortName(Candidate candidate[])
  {
    int j;
    boolean finished = true; 
    String temp;
    while ( finished )
    {
        finished = false;
        for (j=0;j<candidate.length-1;j++)
        {
             if (candidate[j].getName().compareTo(candidate[j+1].getName())>0)
                {                                            
                   temp = candidate[j].getName();
                   candidate[j].getName() = candidate[j+1].getName();
                   candidate[j+1].getName() = temp; 
                   finished = true;
                       } 
               } 
        } 
  } 

The lines 2nd/3rd lines in the if statement are giving me errors saying it's expecting a variable but finding a value. I suspect it's something to do with trying to access the private name field in the Candidate class through .getName(), but I'm unsure of how else to do this. Any help?

EDIT: I'm now getting an NPE error on the following line

if (candidate[j].compareTo(candidate[j+1])>0)

Here is my compareTo method from my Candidate class.

public int compareTo(Candidate candidate)
{
    int candidateCompare = this.getName().compareTo(candidate.getName());         
    if (candidateCompare != 0)
        return candidateCompare;
    else{
        if (this.getName().compareTo(candidate.getName()) < 0)
           return -1;
        if (this.getName().compareTo(candidate.getName()) > 0)
            return 1;
    }
    return 0;
}

I do not understand why this is happening, tried researching a solution but found no help.

EDIT 2: I have the following lines in my do-while loop where I get user input for the data fields of my class. In this loop, the a new object with the data is created, and assigned to my object array named origCandidateList. Here is the code.

 origCandidateList[candidateCount] = new Candidate(candidateName, candidateAge, candidateParty, candidateIssue);
 candidateCount++;

I initially had the first line below the candidateCount increment, so the array was starting at 1 instead of 0, but this did not fix the issue. I still get an NPE error. The array prints out fine, so the data is going into it. But something seems to be going awry with my compareTo method.

Upvotes: 0

Views: 113

Answers (3)

iskandarchacra
iskandarchacra

Reputation: 368

Try saving your variables in a String before assigning new values to them.

String temp = canditate[j].getName();
String firstCandName = candidate[j].getName();
String secondCandName = canditate[j+1].getName();
firstCandName = secondCandName;
secondCandName = temp;

The reason why for example candidate[j].getName() = temp, doesn't work is because candidate[j].getName() is not a variable, and the left side of the argument should be a variable, therefore this will give you an error. Try doing what I did above it should work fine for you. Just to clarify what I mean by candiate[j].getName() is not a variable. getName() method returns a String therefore this whole function is a String. So when you put it on the left side of an equality and try to modify it, Java wouldn't know what to modify. It is like saying

"Sample String" = "Another Sample String" instead of,

String sampleString = "Sample String";
sampleString = "Another Sample String";

Although this should work for you, I personally think you are better off swapping the objects inside the array.

Upvotes: 0

Chuck McKnight
Chuck McKnight

Reputation: 119

Not to be disrespectful, but is there a reason you want to do a bubble sort instead of using the built-in sort mechanism? The bubble sort is horribly inefficient for anything but a relatively small dataset.

Regardless, you need to remember that Java (generally) works with objects, so you should swap the objects in the array rather than trying to switch an object member.

Upvotes: 0

Waggili
Waggili

Reputation: 371

You can't assign values to candidate[j].getName(). There are no "L-Values" like in C++, where you can do stuff like that (e.g. myVector.at(j) = newValue;).

You must use something like candidate[j].SetName(candidate[j+1].getName());

Better yet, switch out the whole object by doing candidate[j] = candidate[j+1].

Upvotes: 2

Related Questions