Vlad Alexeev
Vlad Alexeev

Reputation: 2204

Java sorting ArrayList<Integer>

Well , I'm stock on something very simple but I can't figure it out.

First off, I know that there is Collection.sort() method, but my ArrayList is sort of links to data to main object, and my sorting is needed to be made according to this object's data.

Like this is a sport competition and ArrayList<Integer> numbers is keeping numbers of participants that has passed a checkpoint.

And I need to sort this ArrayList by the best time from min to max to set them on who's on 1st place, 2nd etc.

for this I should ask my Competiton object :

    public ArrayList<Integer> sort (ArrayList<Integer> numbers)
    for (int i=0;i<numbers.size){
    long time = competition.participant.get(numbers.get(i)).getTimeOfLastCheckPoint();
    /*Do something to make another ArrayList<Integer> sortedArray
 where all this will be sorted by this time parameter from minimal to maximum*/
    return sortedArray;
    }

this is not the actual code, but you've got the idea. I stuck with trying to find seemingly easy solution. Please Help

Upvotes: 0

Views: 5063

Answers (4)

My-Name-Is
My-Name-Is

Reputation: 4942

For me, this sounds like a workaround solution for a half-done SQL query. In case that your data resides in a data base (and I'm pretty sure that this is the case), modify your SQL- query so that you don't have to do that sorting of data at application level. This is good for at least two reasons:

  1. Simplyfiy application logic
  2. Speed up execution (The data base can do such sorting much faster)

Upvotes: 2

isnot2bad
isnot2bad

Reputation: 24444

Write a java.util.Comparator that compares Integers by using them as index in your participants-array:

public class ParticipantIndexComparator implements Comparator<Integer> {
    final List<Participant> participants;
    public ParticipantIndexComparator(List<Participant> participants) {
        this.participants = participants;
    }

    @Override
    public int compare(Integer i1, Integer i2) {
        long l1 = participants.get(i1).getTimeOfLastCheckPoint();
        long l2 = participants.get(i2).getTimeOfLastCheckPoint();
        return Long.compare(l1, l2);
    }
}

Now you can use this comparator to sort your integers:

Collections.sort(numbers, new ParticipantIndexComparator(participants));

But before doing so, ask yourself why your list contains Integer-objects that are indices to the participants-list, instead of the Participants themselves!

Upvotes: 3

rgettman
rgettman

Reputation: 178253

It seems awkward to sort an ArrayList<Integer> based on other things that have nothing directly to do with what you actually want to sort on -- the times.

I would design it differently. It looks you have some kind of object defined on which you can call getTimeOfLastCheckPoint(). For now, I'm assuming it's called Participant. Instead of maintaining an ArrayList<Integer> to store index-based references to your participants, I would maintain an ArrayList<Participant>.

Then I would create a class that implements Comparator<Participant> (perhaps ParticipantComparator) (Comparator javadocs) that knows how to compare Participants based on the results of the call to getTimeOfLastCheckPoint(). Then sorting is simply Collections.sort(participantsArrayList, new ParticipantComparator());.

Upvotes: 3

You can use a Comparator to sort the list according to their race duration and also use for-each loop in Java.

Upvotes: 0

Related Questions