commit 4618ca2123096176c4364825c8cc3865f3941b1e Author: angelsflyinhell Date: Sat Sep 17 19:25:23 2022 +0200 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f43530 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..5825d98 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..06e8b35 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4aa7c8a --- /dev/null +++ b/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + org.example + buchSort + 1.0-SNAPSHOT + + + 17 + 17 + + + + + + org.apache.commons + commons-collections4 + 4.4 + + + + \ No newline at end of file diff --git a/src/main/java/Buch.java b/src/main/java/Buch.java new file mode 100644 index 0000000..b0a95bf --- /dev/null +++ b/src/main/java/Buch.java @@ -0,0 +1,35 @@ +public class Buch { + private String buchID; + private String autor; + private String titel; + private int erscheinungsjahr; + private String genre; + + public Buch(String buchID, String autor, String titel, int erscheinungsjahr, String genre) { + this.buchID = buchID; + this.autor = autor; + this.titel = titel; + this.erscheinungsjahr = erscheinungsjahr; + this.genre = genre; + } + + public String getBuchID() { + return buchID; + } + + public String getAutor() { + return autor; + } + + public int getErscheinungsjahr() { + return erscheinungsjahr; + } + + public String getGenre() { + return genre; + } + + public String getTitel() { + return titel; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..5c6dcfb --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,49 @@ +import java.util.Random; + +public class Main { + public static void main(String[] args) { + Buch[] buchListe = new Buch[26]; + + for (int i = 0; i < buchListe.length; i++) { + buchListe[i] = new Buch(randStr(5), randStr(10), randStr(5), new Random().nextInt(1), randStr(5)); + } + + Buch[] sorted = sortBuecher(buchListe); + + for (Buch buch : sorted) { + System.out.println(buch.getAutor() + ", " + buch.getErscheinungsjahr() + ", " + buch.getTitel()); + } + } + + public static Buch[] sortBuecher(Buch[] buchListe) { + for (int i = 1; i < buchListe.length; i++) { + for (int j = 0; j < buchListe.length - i; j++) { + if (buchListe[j].getAutor().compareTo(buchListe[j + 1].getAutor()) > 0) + swap(buchListe, j, j + 1); + + if (buchListe[j].getAutor().compareTo(buchListe[j + 1].getAutor()) == 0 && buchListe[j].getErscheinungsjahr() > buchListe[j + 1].getErscheinungsjahr()) + swap(buchListe, j, j + 1); + + if (buchListe[j].getAutor().compareTo(buchListe[j + 1].getAutor()) == 0 && buchListe[j].getErscheinungsjahr() == buchListe[j + 1].getErscheinungsjahr() && buchListe[j].getTitel().compareTo(buchListe[j + 1].getTitel()) > 0) + swap(buchListe, j, j + 1); + } + } + return buchListe; + } + + public static Buch[] swap(Buch[] buchListe, int i, int j) { + Buch temp = buchListe[i]; + buchListe[i] = buchListe[j]; + buchListe[j] = temp; + return buchListe; + } + + public static String randStr(int length) { + String letters = "abcdefghijklmnopqrstuvwxyz"; + String random = ""; + for (int i = 0; i < length; i++) { + random += letters.toCharArray()[new Random().nextInt(2454) % letters.length()]; + } + return random; + } +}