简体   繁体   English

Java排序ArrayList <Integer>

[英]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. 首先,我知道有Collection.sort()方法,但是我的ArrayList是对指向主对象的数据的链接的排序,并且需要根据该对象的数据进行排序。

Like this is a sport competition and ArrayList<Integer> numbers is keeping numbers of participants that has passed a checkpoint. 像这样的体育比赛, ArrayList<Integer> numbers将保持通过检查点的参与者的数量。

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. 我需要按照从最小到最大的最佳时间对该ArrayList进行排序,以将其设置在第一名,第二名等上。

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. 根据其他与您实际要排序的时间没有直接关系的东西对ArrayList<Integer>进行排序似乎很尴尬。

I would design it differently. 我会设计不同。 It looks you have some kind of object defined on which you can call getTimeOfLastCheckPoint() . 看起来您已经定义了某种对象,可以在其上调用getTimeOfLastCheckPoint() For now, I'm assuming it's called Participant . 现在,我假设它称为Participant Instead of maintaining an ArrayList<Integer> to store index-based references to your participants, I would maintain an ArrayList<Participant> . 我将维护一个ArrayList<Participant> ,而不是维护ArrayList<Integer>来存储对参与者的基于索引的引用。

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() . 然后,我将创建一个实现Comparator<Participant> (也许是ParticipantComparator )( Comparator javadocs )的类,该类知道如何基于对getTimeOfLastCheckPoint()的调用结果来比较Participant Then sorting is simply Collections.sort(participantsArrayList, new ParticipantComparator()); 然后排序就是Collections.sort(participantsArrayList, new ParticipantComparator()); .

Write a java.util.Comparator that compares Integer s by using them as index in your participants-array: 编写一个java.util.Comparator ,通过将Integer用作参与者数组中的索引来比较Integer

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! 但在这样做之前,请问自己为什么列表中包含Integer对象,这些对象是参与者列表的索引,而不是Participant自身!

For me, this sounds like a workaround solution for a half-done SQL query. 对我来说,这听起来像是针对半成品SQL查询的解决方法。 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. 如果您的数据位于数据库中(我很确定是这种情况),请修改您的SQL查询,这样您就不必在应用程序级别进行数据排序了。 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循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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