3

I am working on a report module that shows the time spent and number of tasks. The values are set in the Java Bean and the bean object is stored in a array.I am using separate queries to get time and number of tasks.Now i have to sort the array based on time and number of tasks. The code below compares only Strings:

if (!list.isEmpty()) {
    Collections.sort(list, new Comparator<Project>() {
        @Override
        public int compare(Project p1, Project p2) {

            return p1.getName().compare(p2.getName());
        }
       });
   }

I have problems in sorting the integer value of a property in JavaBean which is stored in an array.Any help is greatly appreciated.

10
  • Are you tying to sort integer array, you can use Arrays.sort(int[] a) Commented Feb 19, 2013 at 9:26
  • So what's array look like ? Commented Feb 19, 2013 at 9:26
  • Integer and Date both also have a compareTo method, can't you use them? Commented Feb 19, 2013 at 9:28
  • @Jason: Project[] array = new Project[3]; I am setting the integer value in a property of Project object and then I am storing the project object in the array. This is what you are asking, right? Commented Feb 19, 2013 at 9:38
  • why you should not storing project in the list ? That's easy to sort. Commented Feb 19, 2013 at 9:46

6 Answers 6

7

In your comments below the question you say you have:

Project[] array = new Project[3];

If I were you I'd declare Project as

public class Project implements Comparable<Project> {

    @Override
    public int compareTo(Project other) {
        return this.id - other.id; // or whatever property you want to sort
    }

Then you can sort your array by simply calling

Arrays.sort(array);

If you don't want your class to implement the Comparable interface you can pass a comparator to Arrays.sort():

    Arrays.sort(array, new Comparator<Project>() {
        @Override
        public int compare(Project o1, Project o2) {
            return o1.id - o2.id; // or whatever property you want to sort
        }
    });

I used an anonymous one, you could also extract it to it's own class if needed elsewhere.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you have a List<Project>, all you need in Java8 (since you can use lambda expression for the Comparator) is:

Collections.sort(list, (o1, o2) -> o1.getName().compareTo(o2.getName()));

And List has a sort(Comparator) method, so you can shorten this even further:

list.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));

With method references it becomes even cleaner:

list.sort(Comparator.comparing(Project::getName));

4 Comments

Who was using public fields?
Assumptions. But you right, I edited the answer. Thanks.
I'm still confused. All of your solutions use accessor methods. Did you mean method references?
Note to reader: Prefer the last of those suggestions, generally.
0

try this to sort integers

return(p1.getIntProperty() - p2.getIntProperty());

Comments

0

Did you look into the Comparator and Comparable interfaces in java. You can use the compare() method to compare the elements in the array and sort them accordingly. You check this post where same question is already answerd Sort Java Collection

Comments

0

How about something like:

Project[] project = ..initialise array;

List<Project> projectList = Arrays.asList(project);
Collections.sort(projectList, new Comparator<Project>() {
    @Override
    public int compare(Project p1, Project p2) {
        if(p1.getIntProperty() == p2.getIntProperty()) return 0;
        return p1.getIntProperty() > p2.getIntProperty() ? 1 : -1;
    }
   });
}

5 Comments

what's happen if two values equal ?
And what if both values are equal? You never return 0.
Updated, wouldve done the job anyway, but seems people dont like it not returning 0. Personally I like the 1 liner
Why convert it to a list, if you can simply sort the array?
I based this on the OPs example of Sorting Strings, assuming he wished to do it the same way. I also think he may be dealing with a list anyway
0

You can pass a comparator to Arrays.sort():

Arrays.sort(array, new Comparator<Project>() {
    @Override
    public int compare(Project o1, Project o2) {
        return o1.id - o2.id; // or whatever property you want to sort
    }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.