1

I have a very problematic problem! I have an array list that looks like this:

 ArrayList<String[]> teamsToOrder = new ArrayList<String[]>();

In each string array that is added to the ArrayList looks like this:

 String[] arrayExample = {teamNumber,score};

 String[] array1 = {"340","100"};
 String[] array2 = {"7680","70"};
 String[] array3 = {"770","110"};
 ...........

What I want to be able to do is organize the arrays by score (best score to worst) like so:

 String[] array3 = {"770","110"};
 String[] array1 = {"340","100"};
 String[] array2 = {"7680","70"};
 ...........

How do I organize them and store them in the ArrayList? I am sorry to mention this, but my objective requires me to organize them in ArrayList form. I can't (i'm not allowed to) create a separate class for the data....

4
  • 7
    Write a custom Comparator and sort the ArrayList. Commented Dec 29, 2013 at 0:57
  • 2
    You should make a class. Commented Dec 29, 2013 at 0:59
  • Can you show me how I would write a Comparator for this sort of problem. I have looked at them as an option, but I haven;t gotten them to work... Commented Dec 29, 2013 at 1:04
  • See DasBlinkenLights' answer for an example of a comparator. Commented Dec 29, 2013 at 1:29

4 Answers 4

9

You can do it like this:

Collections.sort(teamsToOrder, new Comparator<String[]>() {
    public int compare(String[] lhs, String[] rhs) {
        // Parse the scores of the two items
        int leftScore = Integer.parseInt(lhs[1]);
        int rightScore = Integer.parseInt(rhs[1]);
        // Compare the two scores, and return the result
        return Integer.compare(leftScore, rightScore);
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

I don't understand the 'leftScore' and the 'rightScore'..... Can you please enlighten me on how this comparator would sort the lists? In my quesiotn there is a 'teamNumber' which is used as an identifyer, and will not be used to sort out the string[]'s. I only want to sort by the scores....
@littleHelper Your array contains scores as strings. In order to compare them correctly, you need to parse them first, otherwise 100 will sort as "less than" 20, because strings are sorted lexicographically.
The comparator just compares two objects, so that sortingMethod can use the comparator to determine the order of elements.
I understand the parsing portion, but I only want to compare the 'scores' portion of the arrays: Like so: array1[1] to all of the other array socres. And then find out where it will go...
@littleHelper That's what the comparator does. Collections#sort will use the comparator to sort the entire list.
|
4

This was my original post but I edited this to make it more simple:

Here is the way to sort the ArrayList<String[]>:

Collections.sort(teamsToOrder,new Comparator<String[]>() {
    public int compare(String[] s1, String[] s2) {

        return Integer.parseInt(s2[1]) - Integer.parseInt(s1[1]);
    }
});

That's it. So, as an example:

public static void main(String args[]) {

    ArrayList<String[]> teamsToOrder = new ArrayList<String[]>();
    String[] array1 = {"340","100"};
    String[] array2 = {"7680","70"};
    String[] array3 = {"770","110"};

    teamsToOrder.add(array1);teamsToOrder.add(array2);teamsToOrder.add(array3);

    Collections.sort(teamsToOrder,new Comparator<String[]>() {
        public int compare(String[] s1, String[] s2) {

            return Integer.parseInt(s2[1]) - Integer.parseInt(s1[1]);
        }
    });

    // display the new sorted ArrayList of String arrays:
    for (String[] s: teamsToOrder) {
        System.out.println(Arrays.toString(s));
    }
}

Output:

[770, 110]
[340, 100]
[7680, 70]

4 Comments

How do I call the 'compare' method and what do I store the return as to get the organized ArrayList?
@littleHelper I'm not sure what you mean for either question. If you're talking about the Comparator that was mentioned, see the example I wrote. As for your other question, I'm not sure what you mean.
@littleHelper Good. If dasblinkenlight's or myself's answers have helped you with your question, please accept the answer that you think is the best.
Either way thanks soooooooo much! I got it to work and I believe that's all that really matters. It doesn't matter how the code is written as long as it can perform the designated function!
1

You should write a Comparator to compare the Strings the way you want to. Then sort your ArrayList using the implemented comparator. See: http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html

3 Comments

This doesn't really add anything to the exact comment posted by @Sotirios Delimanolis in my opinion
Sorry, but the link you gave me appears a little confusing... Could you possibly give me some sort of example of how I would use a comparator?
Yes. You compare two arrays by their [1] field (second one). If arrayOne[1] > arrayTwo[1] you return 1. If equal -> 0. If smaller -> -1. Put this logic into compare mmethod. I say to compare [1] field, as there you store the scores, as far as I see.
0

You can implement this by the following way. It is advisable to use List interface instead of ArrayList object.

public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<String> entries = new ArrayList<String>();
        entries.add("0 - name1");
        entries.add("1000 - name2");
        entries.add("1004 - name4");
        entries.add("1002 - name3");
        entries.add("10000 - name5");
        entries.add("2000 - name5");

        Comparator<String> comparator = new MyComparator();
        Collections.sort(entries, comparator );

        for (String e : entries){
            System.out.println(e);
        }

    }

Comments

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.