-1

I'm trying to sort the elements in the array list based on the length of the words in the list. so the shortest to the longest. With the code below, it won't get sorted for some reason.

Question - Where is the bug in my implementation?

public static void sort(ArrayList<String> list) {
        for(int i = 0; i <  list.size(); i++) {
            String e1 = list.get(i);
            for(int j = i; j < list.size(); j++) {
                String e2 = list.get(j);
                if( e1.length() > e2.length()) {
                    String tmp = e1;
                    e1 = e2;
                    e2 = tmp;
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
          }
    } 

Edit:

public static void sort(ArrayList<String> list) {
    for(int i = 0; i <  list.size(); i++) {
        String e1 = list.get(i);
        for(int j = i; j < list.size(); j++) {
            String e2 = list.get(j);
            if( e1.length() > e2.length()) {
                String tmp = e1;
                e1 = e2;
                e2 = tmp;
                list.set(j, e1);
                list.set(i, e2);

            }
        }
    }
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
      }
}
AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
rosababy
  • 167
  • 10
  • Take a look at the stack trace. Then [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your program. This should get you to the cause in no time. – Turing85 Jul 02 '19 at 20:13
  • Check the condition on the inner loop. You are checking `i`, not `j`. – Andy Turner Jul 02 '19 at 20:13
  • The problem is with this line: `for(int j = i; i < list.size(); j++)`. It should be `j < list.size()` – Jordan Jul 02 '19 at 20:14
  • 2
    BTW swapping values between `e1` and `e2` doesn't affect `list`. More at [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/q/40480) – Pshemo Jul 02 '19 at 20:15
  • @Pshemo yes the sorting isn't working for some reason... – rosababy Jul 02 '19 at 20:16
  • 1
    @rosababy It isn't sorting because you never change the values in `list`. You need to call `list.set(index, value)` to change the values in the list (replace "index" and "value" with the appropriate index and value). – Jordan Jul 02 '19 at 20:21
  • @Jordan I added list.set(i, e1); list.set(j, e2); but no difference... – rosababy Jul 02 '19 at 20:30
  • Anyway you shouldn't be reinventing the wheel. Lists already gives us `sort` method which expect `Comparator` - a way to decide which of two compared values is bigger, smaller or maybe equal. Comparator also provides `comparing(...)` method which lets us provide Function to *property* based on which you want to compare two values, here it is length() method. So your code can be rewritten as `list.sort(Comparator.comparing(s->s.length()));`. – Pshemo Jul 02 '19 at 20:37
  • You're setting the values in the list to just be the same values that they already are. Try this: `list.set(i, e2);` and `list.set(j, e1);`. – Jordan Jul 02 '19 at 20:38
  • @Jordan thanks but it still doesn't work. I pasted what I have. – rosababy Jul 02 '19 at 20:41
  • You are swapping them, but pasting them in the opposite locations (essentially swapping them back). If you use those `set` calls, you don't need the three lines above them. – Mike Harris Jul 02 '19 at 20:43
  • @MikeHarris I'm sorry I'm having trouble understanding over the words....do you mind posting on the answer? – rosababy Jul 02 '19 at 20:45
  • Since `e1 = list.get(i), e2 = list.get(j)` we can say that `i` is original index of `e1` and `j` original index of `e2`. On which positions should you write them if `e1>e2`? If we want to swap them in list we should now write `e1->j` and `e2->i` and that is what you have in your code. Unfortunately right before that you also swap content of `e1` and `e2` (remember how you use that `tmp` variable?). You see the problem now? – Pshemo Jul 02 '19 at 20:57

3 Answers3

0

In the second for loop, change condition to

for(int j = i; j < list.size(); j++)
grnc
  • 477
  • 6
  • 13
0

On the last pass of the inner loop, it will be out of bounds. Think of it this way, on the last pass of the outer loop where it should stop, you make an additional pass with the inner loop.

It also doesn't sort your list because the strings you are comparing aren't in your list.

w8std
  • 24
  • 2
0

Remove the part where you exchange string values:

enter image description here

you're already swapping the indexes. I tried out the code without this part, it seemed to work. Try removing this part and let me know about the result.

AntiqTech
  • 717
  • 1
  • 6
  • 10