Shlomi Shabtai
Shlomi Shabtai

Reputation: 31

Trying to sort an Array based on another Array alphabetical order

I've got 2 ArryLists: an Integers one and a Strings one.

ArrayList<String> stars = new ArrayList<>();
ArrayList<Integer> index = new ArrayList<>();

index.add(1);
index.add(2); 
index.add(3); 
index.add(4); 
index.add(5); 
index.add(6);

stars.add("g");
stars.add("c"); 
stars.add("a"); 
stars.add("l"); 
stars.add("b"); 
stars.add("q");

I need to rearrange the values of the index ArryList such that stars.get(index.get(i)) will give the stars Array in Alphbetical order

I worte this code:

for (int x = 0; x < stars.size(); x++) {
    for (int y = x+1; y < stars.size(); y++) {
        if (stars.get(x).compareTo(stars.get(y)) > 0 ) {
             int temp = index.get(x);
             index.set(x, index.get(y));
             index.set(y, temp);
        }
    }
}

but I keep getting this output:

[4 2 0 1 3 5]

for the index Array

instead of:

[2 4 1 0 3 5]

can anyone help find what I did wrong?

Upvotes: 1

Views: 134

Answers (4)

Moritz Petersen
Moritz Petersen

Reputation: 13057

If you already have the stars list, then you can do this:

Collection<Integer> values = new TreeMap<String, Integer>() {{
  stars.forEach(key -> put(key, size()));
}}.values();

What's happening here?

  1. Create an anonymous TreeMap, which is a sorted map by order of the keys.
  2. The two {{ }} are an anonymous initializer of the map.
  3. For each entry of stars, add the key with the appropriate index (size() is getting incremented with each put(), starting with 0).
  4. Get the values.

Results:

[2, 4, 1, 0, 3, 5]

But assuming, you need to keep the index list, too, and you want to implement the sorting yourself, then you need to swap the stars and index:

for (int x = 0; x < stars.size(); x++) {
  for (int y = x + 1; y < stars.size(); y++) {
    if (stars.get(x).compareTo(stars.get(y)) > 0) {
      Collections.swap(stars, x, y);
      Collections.swap(index, x, y);
    }
  }
}

Which will result in:

[3, 5, 2, 1, 4, 6]

It seems like you want to have a 0-based index, instead (but seriously: why initialize the index list with a 1?), then you need to subtract 1 of each index value:

System.out.println(index.stream().map(i -> i - 1).collect(Collectors.toList()));

Upvotes: 0

jalynn2
jalynn2

Reputation: 6457

You are sorting the indexes, but not using them to fetch the values to compare. Change your code to use the index values to get the stars value:

for (int x = 0; x < stars.size(); x++) {
    for (int y = x+1; y < stars.size(); y++) {
        if (stars.get(index.get(x)-1).compareTo(stars.get(index.get(y)-1)) > 0 ) {
            int temp = index.get(x);
            index.set(x, index.get(y));
            index.set(y, temp);
        }
    }
}

Note: the -1 is done because you are populating your index array from 1 through 6 instead of 0 through 5. This gives the result [3 5 2 1 4 6]. If your expected answer is [2 4 1 0 3 5], remove the -1 from the fetches and populate your index array with 0 through 5:

index.add(0);
index.add(1);
index.add(2);
index.add(3);
index.add(4);
index.add(5);

for (int x = 0; x < stars.size(); x++) {
    for (int y = x+1; y < stars.size(); y++) {
        if (stars.get(index.get(x)).compareTo(stars.get(index.get(y)) > 0 ) {
            int temp = index.get(x);
            index.set(x, index.get(y));
            index.set(y, temp);
        }
    }
}

Upvotes: 1

b.GHILAS
b.GHILAS

Reputation: 2303

Your're just swapping the elements at position x and y of the index list, you must also swap the values in the star list

for (int x = 0; x < stars.size(); x++) {
    for (int y = x + 1; y < stars.size(); y++) {
        if (stars.get(x).compareTo(stars.get(y)) >= 0 ) {
            int temp = index.get(x);
            index.set(x, index.get(y));
            index.set(y, temp);

            String strTmp = stars.get(x);
            stars.set(x, stars.get(y));
            stars.set(y, strTmp);
        }
    }
}

Edit: If you can't change the star list then you can use this code

for (int x = 0; x < stars.size(); x++) {
    for (int y = x + 1; y < stars.size(); y++) {
        if (stars.get(index.get(x) - 1).compareTo(stars.get(index.get(y) - 1)) >= 0 ) {
            int temp = index.get(x);
            index.set(x, index.get(y));
            index.set(y, temp);
        }
    }
}

for (int i = 0; i < index.size(); i++) {
    System.out.print(stars.get(index.get(i) - 1) + " ");
}

This gives the result

a b c g l q 

Upvotes: 1

Vlad Zaichyk
Vlad Zaichyk

Reputation: 135

Don`t really know what should you do, to get this:

[2 4 1 0 3 5]

But if you put these values in sorted map:

Map<String, Integer> map = new TreeMap<>();
    map.put("g", 1);
    map.put("c", 2);
    map.put("a", 3);
    map.put("l", 4);
    map.put("b", 5);
    map.put("q", 6);
    System.out.println(map.values());

you`ll get this result:

[3, 5, 2, 1, 4, 6]

Upvotes: 1

Related Questions