对按保持排序的值排序的数组列表进行排序

我有一个对象的 ArrayList,在每个对象中我都有一个日期和一个小时,我已将其转换为 int 以进行排序。我已经按日期对对象列表进行了排序,现在我想按保持日期排序的小时对同一数组列表进行排序。

这种情况的一个小例子:

我有一个带有方法 getDateEvent() 和 getHourEvent() 的事件对象,它返回一个可排序的数据和小时值,如 YYYYMMDD 和 HHMM

事件的数组列表已经按日期排序,所以我必须按小时排序数组列表,以保持日期排序。

我的代码实际上是这样的:

private ArrayList<Evento> SortingHour(ArrayList<Evento> SortingList){

    int t=0;

    int counter=0;

    ArrayList<Evento> finale = new ArrayList<>();

    ArrayList<Evento>[] Array =  new ArrayList[SortingList.size()];

    while(counter<SortingList.size()){

        if( SortingList.get(counter).getDateEvent()).equals(SortingList.get(counter+1).getDateEvent()))) {

            Array[t].add(SortingList.get(counter));

            Array[t].add(SortingList.get(counter+1));

            counter++;


        }else{

            t++;

        Array[t].add(SortingList.get(counter));}


        counter++;

        }


            for(int c=0;c<t;c++) {

                for (int c1 = 0; c1 < Array[c].size(); c1++) {

                    for (int c2 = 0; c2 < Array[c].size(); c2++) {

                        if (Integer.parseInt(Array[c].get(c1).getHourEvent()) < Integer.parseInt(Array[c].get(c2).getHourEvent())) {

                            Collections.swap(Array[c], c1, c2);

                        }


                    }

                }

            }

        for(int c=0; c<t;c++){

            finale.addAll(Array[c]);

        }


    return finale;

    }


30秒到达战场
浏览 177回答 2
2回答

子衿沉夜

尝试这样的事情:public ArrayList<Item> sort(List<Item> list) {&nbsp; &nbsp; ArrayList<Item> dateSort= new ArrayList<>();&nbsp; &nbsp; ArrayList<Item> result = new ArrayList<>();&nbsp; &nbsp; if (list!= null && list.size() != 0) {&nbsp; &nbsp; &nbsp; dateSort= new ArrayList<>(list);&nbsp; &nbsp; &nbsp; Collections.sort(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dateSort, (o1, o2) -> Integer.compare(o1.getIntDate(), o2.getIntDate()));&nbsp; &nbsp; &nbsp; int currentDate = dateSort.get(0).getIntDate();&nbsp; &nbsp; &nbsp; ArrayList<Item> temp= new ArrayList<>();&nbsp; &nbsp; &nbsp; for (int i = 1; i < dateSort.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (dateSort.get(i).getIntDate() > currentDate ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentDate = dateSort.get(i).getIntDate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.addAll(timeSort(temp));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.add(dateSort.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.add(dateSort.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;&nbsp; }private ArrayList<Item> timeSort(List<Item> list) {&nbsp; ArrayList<Item> timeSort= new ArrayList<>();&nbsp; if (list!= null && list.size() != 0) {&nbsp; &nbsp; &nbsp; timeSort= new ArrayList<>(list);&nbsp; &nbsp; &nbsp; Collections.sort(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeSort, (o1, o2) -> Integer.compare(o1.getIntTime(), o2.getIntTime()));&nbsp; }&nbsp; return timeSort;}

牧羊人nacy

我创建了以下示例,public class Timings {&nbsp; &nbsp; private LocalDate date;&nbsp; &nbsp; private int hour;&nbsp; &nbsp; public Timings(LocalDate date, int hour) {&nbsp; &nbsp; &nbsp; &nbsp; this.date = date;&nbsp; &nbsp; &nbsp; &nbsp; this.hour = hour;&nbsp; &nbsp; }&nbsp; &nbsp; public LocalDate getDate() {&nbsp; &nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; &nbsp; public int getHour() {&nbsp; &nbsp; &nbsp; &nbsp; return hour;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Timings{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "date=" + date +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", hour=" + hour +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}public class Sorting {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; LocalDate date = LocalDate.of(2015, 02, 20);&nbsp; &nbsp; &nbsp; &nbsp; LocalDate date1 = LocalDate.of(2018, 07, 12);&nbsp; &nbsp; &nbsp; &nbsp; LocalDate date2 = LocalDate.of(2017, 05, 10);&nbsp; &nbsp; &nbsp; &nbsp; Timings timings = new Timings(date, 10);&nbsp; &nbsp; &nbsp; &nbsp; Timings timings1 = new Timings(date1, 8);&nbsp; &nbsp; &nbsp; &nbsp; Timings timings2 = new Timings(date2, 12);&nbsp; &nbsp; &nbsp; &nbsp; List<Timings> dateList = List.of(timings, timings1, timings2);&nbsp; &nbsp; &nbsp; &nbsp; List<Timings> newList = dateList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted( (a1, a2) -> a1.getDate().compareTo(a2.getDate()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.comparingInt(Timings::getHour))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(newList);&nbsp; &nbsp; }}在上面,第一种排序方法负责根据日期进行排序。第二个按小时照顾。我得到以下输出[Timings{date=2018-07-12, hour=8}, Timings{date=2015-02-20, hour=10}, Timings{date=2017-05-10, hour=12}]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java