Java Sorting "queue" list based on DateTime and Z Position (part of school project)

Posted by Kuchinawa on Stack Overflow See other posts from Stack Overflow or by Kuchinawa
Published on 2012-12-14T04:43:59Z Indexed on 2012/12/14 5:03 UTC
Read the original article Hit count: 106

Filed under:
|
|
|
|

For a school project i have a list of 50k containers that arrive on a boat. These containers need to be sorted in a list in such a way that the earliest departure DateTimes are at the top and the containers above those above them. This list then gets used for a crane that picks them up in order.

I started out with 2 Collection.sort() methods:

1st one to get them in the right X>Y>Z order

Collections.sort(containers, new Comparator<ContainerData>() 
{
    @Override
    public int compare(ContainerData contData1, ContainerData contData2)
    {
        return positionSort(contData1.getLocation(),contData2.getLocation());
    }
});

Then another one to reorder the dates while keeping the position in mind:

Collections.sort(containers, new Comparator<ContainerData>() 
{
    @Override
    public int compare(ContainerData contData1, ContainerData contData2)
    {
        int c = contData1.getLeaveDateTimeFrom().compareTo(contData2.getLeaveDateTimeFrom());

        int p = positionSort2(contData1.getLocation(), contData2.getLocation());

        if(p != 0)
            c = p;

        return c;
    }
});

But i never got this method to work..

What i got working now is rather quick and dirty and takes a long time to process (50seconds for all 50k):

First a sort on DateTime:

Collections.sort(containers, new Comparator<ContainerData>() 
{
    @Override
    public int compare(ContainerData contData1, ContainerData contData2)
    {
        return contData1.getLeaveDateTimeFrom().compareTo(contData2.getLeaveDateTimeFrom());
    }
});

Then a correction function that bumps top containers up:

containers = stackCorrection(containers);

private static List<ContainerData> stackCorrection(List<ContainerData> sortedContainerList)
{
    for(int i = 0; i < sortedContainerList.size(); i++)
    {
        ContainerData current = sortedContainerList.get(i);

        // 5 = Max Stack (0 index)
        if(current.getLocation().getZ() < 5) 
        {   //Loop through possible containers above current
            for(int j = 5; j > current.getLocation().getZ(); --j) 
            {   //Search for container above
                for(int k = i + 1; k < sortedContainerList.size(); ++k)
                    if(sortedContainerList.get(k).getLocation().getX() == current.getLocation().getX())
                    {
                        if(sortedContainerList.get(k).getLocation().getY() == current.getLocation().getY())
                        {
                            if(sortedContainerList.get(k).getLocation().getZ() == j)
                            {   //Found -> move container above current
                                sortedContainerList.add(i, sortedContainerList.remove(k));
                                k = sortedContainerList.size();
                                i++;
                            }
                        }
                    }
                }
            }
        }

    return sortedContainerList;
}

I would like to implement this in a better/faster way. So any hints are appreciated. :)

© Stack Overflow or respective owner

Related posts about java

Related posts about list