Howdy, I do know how to implement a simple bubble-sort for 1dimensional array. But with 2dimensional or multidimensional, that's where I have my problems.
So far I've been using this to sort 1Dimensional Arrays, works like a charm. But mostly with integer numbers, not strings:
boolean sort;
do{
sort = true;
for (int i = 0; i < testarray.length - 1; i++){
if(testarray[i] > testarray[i+1]){
temp = testarray[i];
testarray[i] = testarray[i+1];
testarray[i+1] = temp;
sort = false;
}
}
}while(!sort);
// Descending Output
// for (int k = testarray.length - 1; k >= 0 ; k--){
// Ascending Output
for (int k = 0; k < testarray.length ; k++){
System.out.print(testarray[k] + ", ");
}
Assuming I have:
Customernumber, Name, Surname, Address
String customers[][] = {{"123", "John", "Doe", "Somewhere"}, {"007", "James", "Bond", "MI5"}, {"1337", "Lolcat", "Izgud", "Saturn"}}
Now, I want to choose to sort what to sort after: customernumber, name, surname or address. And after that I want to output it ascending or descending, depending what I want.
I just have no idea how to implement this with bubble-sort. I want to stay in bubble-sort, , no other sorting algorithm, I want to learn how bubble-sort works in such a situation.
For ascending and descending my idea would be: I could do an if-Loop. For example if (asc == 1) then output ascending, else output descending
. The asc would then be asked via Console for example.
Any help is much appreciated.