views:

831

answers:

4

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.

+1  A: 

You can keep the same sorting algorithm. It will still be unaware of the 2D array. You have to come up with a comparison function that takes two 1D arrays and says which one is the greater.

arhuaco
Hm? How can I keep it? What when I have 3 customers, and I want them to sort ascending after the name: customer[0][2] = xerxes, customer [1][2] = abrahams, customer [2][2] = cusfolk. So it'll be sortded: abrahams [1][2], cusfolk [2][2] and xerxes [0][2].
NoCanDo
+2  A: 

A 2 dimensional array is basically just a 1 dimensional array consisting of arrays.

Just use the same code you have, only instead of ints, move the inner arrays.

To know if one array is 'bigger' than the next, compare the string values of the correct array member (so the name or surname,..). To do this you can use the String CompareTo method.

Last note: The example you gave is better if the inner array is actually an Object containing the info. This way you can have separate Datatypes for all the fields instead of all making them Strings.

e.g.:

  class Person
  {
       int customerNumber;
       String name;
       String surName;
       String address;
  };

EDIT: to actually answer your question:

change your program as following:

change the temp declaration:

 String [] temp;

and change the line:

 if(testarray[i] > testarray[i+1])

into:

 if(testarray[i][1] > testarray[i+1][1])

than it 'll work and sort on the name

R

Toad
cheers! that did it!
NoCanDo
+2  A: 
Carl Smotricz
+1  A: 

I'm guessing you want "123", "john", "doe" and "somewhere" to be grouped together.

I suggest you use an object, say

public object Person {
    private int id;
    private String name;
    private String surname;
    private String address;
}

adding the usual getters and setters.

you can have an ordinary array of Person objects, sorting them with your bubble sort algorithm. You can create several custom comparators that compare either id, name, surname or address to you bubble sort algo.

The signature of the sort method should be something like

public Person[] bubbleSort(Person[] persons, Comparator comp)
Buhb