简体   繁体   中英

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

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 Participant s based on the results of the call to getTimeOfLastCheckPoint() . Then sorting is simply Collections.sort(participantsArrayList, new ParticipantComparator()); .

Write a java.util.Comparator that compares Integer s 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 Participant s themselves!

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)

您可以使用Comparator根据比赛时间对列表进行排序,也可以使用Java中的for-each循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM