Kompliziertes Sortieren einer ArrayList mit Objekten(Sortieren nach X und Y)

Hag2bard

Bekanntes Mitglied
Hallo,

ich komme momentan nicht weiter.

Ich habe eine ArrayList die Objekte enthält welche nach X und Y Koordinaten sortiert werden sollen.
Das heißt die Liste ist so angeordnet

Objekt1 = x5, y1
Objekt2 = x4, y0
Objekt3 = x5, y0
Objekt4 = x4, y1

Ich möchte es so sortieren dass folgende Reihenfolge dabei raus kommt:

Objekt1 = x4, y0
Objekt2 = x5, y0
Objekt3 = x4, y1
Objekt4 = x5, y1

Wie stelle ich das an?
Es muss nicht der komplizierteste Algorithmus sein, also BubbleSort reicht mir, QuickSort wäre mir zu kompliziert.

Das hier habe ich bisher:

Java:
 public void sort() {
        for (int i = 0; i < blockArrayList.size()-1; i++) {
            int yCounter = 0;
            if (blockArrayList.get(i).getDestinationY() == yCounter) {
                if (blockArrayList.get(i).getDestinationX() < blockArrayList.get(i+1).getDestinationX()){
                    Block temp = blockArrayList.get(i);         //i sichern
                    blockArrayList.set(i, blockArrayList.get(i+1));
                }
            }
        }
    }

Weiter weiß ich nicht. Ich weiß nicht wann ich den yCounter hochzählen soll.
 
Java:
Collections.sort( blockArrayList, new Comparator<Point>() {
       public int compare(Point x1, Point x2) {
         int result = Double.compare(x1.getX(), x2.getX());
         if ( result == 0 ) {
           // both X are equal -> compare Y too
           result = Double.compare(x1.getY(), x2.getY());
         }
         return result;
      }
    });
 
Java:
Collections.sort(blockArrayList, Comparator
        .comparingDouble(Point::getX)
        .thenComparingDouble(Point::getY));
 
Danke schonmal für die Antwort. Das Collection Framework ist schon ganz schön mächtig.
Ich bin aber auf folgendes Problem gestoßen:
Anhang anzeigen 18019
Im Code seh ich auch nichts von meinem destinationX und destinationY. Das Block Objekt enthält ja mehrere Felder.
Ich habe die comparingDouble Methode und die thenComparingDouble Methode zu Int geändert, meine X und Y Werte sind integer.


Java:
public class Block {
    private int sourceX;
    private int sourceY;
    private int destinationX;
    private int destinationY;
}




edit: Ich habe meine Block Klasse umgemodelt und aus den zwei int Werten ein Point Wert gemacht, also so hier:

Java:
 private Point source;
    private Point destination;
 
Zuletzt bearbeitet:
da wirst du in der Klasse Block die "compareTo" Methode überschreiben müssen um damit fest zulegen nach was du sortieren willst.

wenn "int" sind ist comparingInt ist bestimmt auch besser als comparingDouble
 
Zuletzt bearbeitet:
Nochmal von Anfang.


Also folgendes. Meine Block Klasse sieht so aus, ich habe sie umgemodelt:

Java:
import java.awt.*;

public class Block {
    private Point source;
    private Point destination;


    public Block(int sourceX, int sourceY, int destinationX, int destinationY) {
        this.source = new Point(sourceX, sourceY);
        this.destination = new Point(destinationX, destinationY);
    }

    public Point getSource() {
        return source;
    }


    public Point getDestination() {
        return destination;
    }

    public void setSource(Point source) {
        this.source.x = source.x;
        this.source.y = source.y;
    }

}

Mit eurem Code bin ich dann auf folgendes Problem gestoßen:

1650368381229.png

Sortiert werden soll im Grunde genommen so, dass erstmal die X Werte mit Y0 aufsteigend sortiert sind,
dann bei y1 dann bei y2 usw...

Wie überschreibe ich am besten die Methode?
 
Danke schonmal für die Antwort. Das Collection Framework ist schon ganz schön mächtig.
Ich bin aber auf folgendes Problem gestoßen:
Anhang anzeigen 18019
Im Code seh ich auch nichts von meinem destinationX und destinationY. Das Block Objekt enthält ja mehrere Felder.
Ich habe die comparingDouble Methode und die thenComparingDouble Methode zu Int geändert, meine X und Y Werte sind integer.


Java:
public class Block {
    private int sourceX;
    private int sourceY;
    private int destinationX;
    private int destinationY;
}




edit: Ich habe meine Block Klasse umgemodelt und aus den zwei int Werten ein Point Wert gemacht, also so hier:

Java:
 private Point source;
    private Point destination;
Der gezeigte Code war nur ein Beispiel, da niemand deinen genauen Code kennt:

Java:
Collections.sort(blockArrayList, Comparator
        .comparingInt(Block::getDestinationY)
        .thenComparingInt(Block::getDestinationX));
 
das point::getx war eine Methoden referenz da du aber diese Methode nicht hast wurde der Fehler geworfen... so wie esoneixe geschrieben hat soll es klappen dass du die destination Methoden benutzt
 

Zurück
Oben