1
String[][] 2dArray = new String[counter][2];
2dArray [counter][column1] = String.valueOf(counter);
2dArray [counter][column2] = "something something something";
for(int i = 0; i < 2dArray.length-1; i++){
     for(int j = i + 1; j > 0; j--){
       if(2dArray[i][j] < 2dArray[i-1][j]){ 
           int[][] temp = 2dArray[i-1][j];
           2dArray[i-1][j] = 2dArray[i][j];
           2dArray[i][j] = temp;                  
       }
     }
}

Attempting to sort the array so that column 1 is ascending. I've studied the other references on here and mimic'd them but for some reason my IDE does not like the above...

8
  • 1
    "my IDE does not like the above" If you get errors/exceptions, please post them in your question. Commented Apr 25, 2014 at 3:34
  • In the if statement the error is; The operator < is undefined for the argument type(s) java.lang.String, java.lang.String. Then the swap of array to temp and temp to array does not like the int/string mismatch. Commented Apr 25, 2014 at 3:35
  • That means a String can't be less than some other String (or at least the 2 can't be compared this way). Commented Apr 25, 2014 at 3:35
  • 1
    If you have a string like "1" and you want it's int value, you can use Integer.valueOf(someString) (where someString is the string you pulled out of the array). Commented Apr 25, 2014 at 3:38
  • 1
    String class has compareTo method.use that. you can't use < to compare strings Commented Apr 25, 2014 at 3:38

5 Answers 5

1

If I understand you correctly, I would suggest the following:

What you would need to do is to compare the Integer values of the array:

if(Integer.valueOf(2dArray[i][0]) < Integer.valueOf(2dArray[i-1][0])){

The reason you don't include j is because you are only sorting by the value of the first column. 2dArray[i][0] gets you the value of your counter at that particular row.

I've also seen some other stuff in your code that could use fixing:

for(int i = 0; i < 2dArray.length; i++){
  for(int j = i; j < 2dArray.length; j++){
    if(Integer.valueOf(2dArray[j][0]) > Integer.valueOf(2dArray[j+1][0])){
       String temp[] = 2dArray[j+1];
       2dArray[j+1] = 2dArray[j];
       2dArray[j] = temp;                  
    }
  }
}

This is more in line with what I think is the classic implementation of BubbleSort:

private static void bubblesort(Integer[] array) {
    for (int i = 0; i < array.length; i++) {
        for(int j = 0; j < array.length - 1; j++) {
            if(array[j].compareTo(array[j+1]) > 0) {
                swap(j, j+1, array);
            }
        }
    }

}

private static void swap(Integer index1, Integer index2, Integer[] array) {
    if(index1 == index2)return;
    Integer temp = new Integer(array[index2]);
    array[index2] = array[index1];
    array[index1] = temp;

}

Except in your case, I'm treating your array as a one-dimensional, since you are only sorting by one dimension.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, Jeeter. I understand the bubble sort when it comes to one dimensional arrays but 2D's are throwing me off. So if I understand your correction to my code, the second for loop specifically focuses on comparing the first column values? And when you call the swap method, or in my case swap within the nested for, you don't have to reference the array structure like array[][]?
Exactly. You only need to compare the first column, so you can treat it as a one-d array. When you say int temp[] = 2dArray[j-1];, its basically the same, you're just swapping a row instead of a datum
hmm... its not liking; if(array[j].compareTo(array[j+1]) > 0), compiler states: Cannot invoke compareTo(String[]) on the array type String[]
@PadawanDeveloper you are using the generic implementation. You should use the topmost implementation for your case
should int temp[] = 2dArray[j-1]; be string temp []? I'm a newb, I know, hoping one day this will become second nature. I'm also looking forward to the day that I work with a team of developers as this remote schooling with no brainstorming sessions is for the birds.
|
1

If 2dArray is a two dimensional String array like

String[][] 2dArray

then the two strings can be compared using

if(2dArray[i][j].compareTo(2dArray[i-1][j]) > 0)

Comments

1

Would this also fix it ?

for (int i = 0; i < 2dArray.length-1; i++){

          for(int j = 0; j < 2dArray.length-1; j++){

              if(2dArray[j][0].compareTo(2dArray[j+1][0])>0){

                String[] temp = 2dArray[j];
                2dArray[j] = 2dArray[j+1];
                2dArray[j+1] = temp;                  
              }
          }
        }

5 Comments

This would work too. It's basically the same, just a different way to say it.
jeeter - i'm still receiving an exception from the compiler, any chance you can throw your solution into a compiler to test it? I would paste my other code in here but its working without the array 'for' bubble sort so its got to be within the 'for'.
Also I'm starting to read about the Collections class library, there's articles and posts that discuss how using Collections and Array Lists is a much better approach in today's world. Any validity there? Wondering if I should be studying up on Collections.reverse() using an Array List instead? Keep in mind I'm in a Java class so right now I'm still learning the fundementals...
The thing with ArrayLists is that they take up much more memory and processing power to construct and access. I'm also on the go at the moment, so i don't have access to an ide. Will post an update soon, though
ok thank you!! Another poster suggested that I declare a class with two attributes for this problem and store the item# and item description in just one array vs. 2. I didn't think about it but that seems like a good approach and would make my sorting simpler, at least conceptually.
-1
for(int i=0;i<2dArray.length-2;i++){
    for(int j=0;j<2dArray[i].length-1;j++){
            if(2dArray[i][j]>2dArray[i+1][j]){
                int temp=2dArray[i][j];
                2dArray[i][j]=2dArray[i+1][j];
                2dArray[i+1][j]=temp;
            }
    }
}

3 Comments

This code would produce the same error that the OP has already gotten. (e.g. you are using > to compare 2 strings).
It really depends ... The guy may just need a hint to fix the problem.
@MingtaoZhang If what you post doesn't solve the problem, then it isn't an answer (e.g. don't post it).
-1
char[][] temp = 2dArray[i-1][j];

Your IDE doesn't like you (120%) when you put a char/int/... (per first response) into a two dimension char array.

4 Comments

What indicates that 2dArray contains chars?
Good question. Bubble sort (+60%), IDE not happy (+30%).
Neither one of those reasons is even close to valid for saying that the array contains chars.
2dArray[i][j] < 2dArray[i-1][j], looks like we can't compare array. "The operator > is undefined for the argument type(s) char[], char[]"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.