Search code examples
javaarrayssortingdatetimebubble-sort

I have an array of objects and each object has been assigned a date, how can I sort these objects in order of date?


I currently have an array containing objects called 'People'. Each one has a name, day, month, year (of birth). I want to convert these to dates and use the .isAfter() method to compare them and resort them but it does not sort them by date at all. Here is a simpler version of the code

    for (int i = 0; i<peopleArray.size()-1; i++)
    {
        for (int j = 0; j<peopleArray.size()-1; j++)
        {

            LocalDate firstDate = LocalDate.of(Integer.parseInt(peopleArray.get(i).getDOBYear()), 
                                      Integer.parseInt(peopleArray.get(i).getDOBMonth()), 
                                      Integer.parseInt(peopleArray.get(i).getDOBDay()));


            LocalDate secondDate= LocalDate.of(Integer.parseInt(peopleArray.get(j).getDOBYear()), 
                  Integer.parseInt(peopleArray.get(j).getDOBMonth()), 
                  Integer.parseInt(peopleArray.get(j).getDOBDay()));

            if(firstDate.isAfter(secondDate)) 
            {
                Person temp = peopleArray[i];
                peopleArray[i] = peopleArray[i+1];
                peopleArray[i+1] = temp;
             }              
        }
    }

'Person' is the name of the object. Thanks a lot for your help in advance!


Solution

  • If your looking for a simpler solution in java, then solution by user2004685 should do.

    Since you've tagged bubble-sort, I'm guessing you need help with the issue in your code.

    Issues:

    1. The loops will never cover the last element in the list. It should run till i/j < size().
    2. The Swap block swaps different elements than the compared elements. You're comparing elements at i and j location but swapping i and i+1
    3. I'm assuming the swap code which access the Array using subscript operator is working on another variable and not the list variable (As Subscript is not applicable for list).
    4. You can enhance the loop by reducing the loop duration. Refer to bubble-sort code provided here for reference.

    Code with Correction:

    for (int i = 0; i<peopleArray.size(); i++)
    {
        for (int j = 0; j<peopleArray.size(); j++)
        {
            LocalDate firstDate = LocalDate.of(Integer.parseInt(peopleArray.get(i).getDOBYear()), 
                                      Integer.parseInt(peopleArray.get(i).getDOBMonth()), 
                                      Integer.parseInt(peopleArray.get(i).getDOBDay()));
    
            LocalDate secondDate= LocalDate.of(Integer.parseInt(peopleArray.get(j).getDOBYear()), 
                  Integer.parseInt(peopleArray.get(j).getDOBMonth()), 
                  Integer.parseInt(peopleArray.get(j).getDOBDay()));
    
            if(firstDate.isAfter(secondDate)) 
            {
                Person temp = peopleArray[i];
                peopleArray[i] = peopleArray[j];
                peopleArray[j] = temp;
             }              
        }
    }