1

Here record is an ArrayList of objects of the type Employee. I am trying to sort the ArrayList based on the employeeName attribute. Unfortunately, it gives unwanted results.

public void sortByName(){       
    for(int i = 0; i < SalesDataManager.N; i++){
        for(int j = i+1; j < SalesDataManager.N; j++){              
            if(record.get(i).getEmployeeName().compareToIgnoreCase(record.get(j).getEmployeeName()) > 0){                           
                Employee etemp = record.get(i);
                record.add(i,record.get(j));
                record.add(j,etemp);                    
            }                       
        }           
    }       
    displayAllRecords();
}

I have gone through other posts in stackoverflow regarding this topic and found out that most of the post suggest this same way.Am I doing something wrong here?

Thanks in advance!

6
  • 3
    what is the expected result? & what is the output? Commented May 14, 2015 at 5:35
  • 1
    Use Collections.sort and pass in a comparator Commented May 14, 2015 at 5:40
  • Is modifying the list as iterating through it your intended action? Commented May 14, 2015 at 5:41
  • 1
    It looks like that you want to swap record.get(i) and record.get(j); however, what record.add(position, element) does is inserting the element at the specified position and shifting the original element at that position and any subsequent elements to the right. Commented May 14, 2015 at 5:47
  • @HowardWang exactly. You spotted it! I want to swap the two objects and I am getting repeated values. Could you suggest something ? Commented May 14, 2015 at 5:50

2 Answers 2

3

You are iterating the record list and according to some condition determining where to add the current element. However, you aren't emptying the list first, so this approach will inevitably lead to duplications.

Java, luckily, has a built it mechanism for such sorting - you just need to implement a Comparator:

public class EmployeeNameComparator implements Comparator<Emplyoee> {
    @Override
    public int compare (Employee a, Employee b) {
        return a.getEmployeeName().compareToIgnoreCase(b.getEmployeeName());
}

And then just use it:

Collections.sort (record, new EmployeeNameComparator());

If you're using Java 8, you could also use the new cleaner syntax:

Collections.sort
  (record, (a, b) -> a.getEmployeeName().compareToIgnoreCase(b.getEmployeeName());
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, I cannot use the inbuilt sort method. It's an assignment and I have to do it the classic way! There are duplicates as you have mentioned, which probably is due to the add(index, object) method of ArrayList. I want to swap the position of objects.
0

@Howard Wang and Mureinik, you guys were right. The add(index,object) method added the object to the index and was right-shifting the already existing object, instead of replacing it, which was what I was intending to achieve. So, adding record.remove(i+1); and record.remove(j+1); to the code did the trick!

public void sortBySales()
{



    for(int i = 0; i < SalesDataManager.N; i++)
    {
        for(int j = i+1; j < SalesDataManager.N; j++)
        {

            if(record.get(i).getEmployeeSales() > record.get(j).getEmployeeSales())
            {


                Employee etemp = record.get(i);
                record.add(i,record.get(j));
                record.remove(i+1);
                record.add(j,etemp);
                record.remove(j+1);

            }   

        }

    }




}

Comments

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.