0

I have an ArrayList of objects, in every object I have a date and an hour that I have casted to int for the sorting. I have already sorted the list of object by the date and now I want to sort the same array list by the hour keeping the sorting of the date.

A little example of the situation:

I have an event object with method getDateEvent() and getHourEvent() that returns a value sortable of data and hour like YYYYMMDD and HHMM

And an arraylist of event already sorted by the date so i have to order the arraylist by the hour keeping the date sorted.

My code is actually this :

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;
    }

2 Answers2

0

Try something like this:

public ArrayList<Item> sort(List<Item> list) {

    ArrayList<Item> dateSort= new ArrayList<>();
    ArrayList<Item> result = new ArrayList<>();

    if (list!= null && list.size() != 0) {

      dateSort= new ArrayList<>(list);

      Collections.sort(
          dateSort, (o1, o2) -> Integer.compare(o1.getIntDate(), o2.getIntDate()));

      int currentDate = dateSort.get(0).getIntDate();

      ArrayList<Item> temp= new ArrayList<>();

      for (int i = 1; i < dateSort.size(); i++) {

        if (dateSort.get(i).getIntDate() > currentDate ) {
          currentDate = dateSort.get(i).getIntDate();
          result.addAll(timeSort(temp));
          temp.clear();
          temp.add(dateSort.get(i));
        }
        else{
          temp.add(dateSort.get(i));
        }

      }
    }
    return result;
  }

private ArrayList<Item> timeSort(List<Item> list) {

  ArrayList<Item> timeSort= new ArrayList<>();
  if (list!= null && list.size() != 0) {

      timeSort= new ArrayList<>(list);

      Collections.sort(
          timeSort, (o1, o2) -> Integer.compare(o1.getIntTime(), o2.getIntTime()));

  }

  return timeSort;
}
Jurij Pitulja
  • 5,546
  • 4
  • 19
  • 25
-1

I've created the below examples,

public class Timings {

    private LocalDate date;
    private int hour;

    public Timings(LocalDate date, int hour) {
        this.date = date;
        this.hour = hour;
    }

    public LocalDate getDate() {
        return date;
    }

    public int getHour() {
        return hour;
    }

    @Override
    public String toString() {
        return "Timings{" +
                "date=" + date +
                ", hour=" + hour +
                '}';
    }
}
public class Sorting {

    public static void main(String[] args) {

        LocalDate date = LocalDate.of(2015, 02, 20);
        LocalDate date1 = LocalDate.of(2018, 07, 12);
        LocalDate date2 = LocalDate.of(2017, 05, 10);

        Timings timings = new Timings(date, 10);
        Timings timings1 = new Timings(date1, 8);
        Timings timings2 = new Timings(date2, 12);

        List<Timings> dateList = List.of(timings, timings1, timings2);

        List<Timings> newList = dateList.stream()
                .sorted( (a1, a2) -> a1.getDate().compareTo(a2.getDate()))
                .sorted(Comparator.comparingInt(Timings::getHour))
                .collect(Collectors.toList());

        System.out.printf(newList);

    }
}

in the above, the first sort method take care the sorting based on the date. The second take cares by the hour.

I got the below output

[Timings{date=2018-07-12, hour=8}, Timings{date=2015-02-20, hour=10}, Timings{date=2017-05-10, hour=12}]

parrotjack
  • 432
  • 1
  • 6
  • 18