-
Sorting arrays
Hi guys
I have an array consisting of 3 parts that I need to sort. The first part is the name of a team, second part is the delimiter (#) and the third part is the teams score.
So for example an element of the array would be LexNeedsHelp#99
I would like to sort the array in order of the score (the 3rd part of the array which will have a maximum score of 99)
Thanks is advance for the help
-
Sorry, I don't understand how your array has 3 parts? Arrays contain one data type.
Do you mean that the elements in your array are Strings and each String has 3 parts: <name>#<number>
You want to sort the elements in the array by the values of the <number> in descending order.
One idea would be a method that returns the <number> part of the String as an int and use that value to do the sort with.
Do you know how to write a sort for the elements of an array? Perhaps you should write a small test program to sort the contents of an int array first to get the technique and then change the code to work with your array of Strings.
-
Think OOP. Write a Team class that has a String field for the team name and an int field for the score. Either implement Comparable or write a Comparator.
Then populate a List with the Team objects and sort the list (Hint: see the methods of the Collection class).
Of course, you may not be allowed to use that approach if this is an educational assignment.
db
-
I have never tried to sort String Arrays according to a sub string like the OP is wanting, but the Arrays.sort( Object[] a, int fromIndex, int toIndex) might fit the bill.
-
The indexes are to select a section of the array.
The OOP aproach is best, but depending on the OPs knowledge who knows what will be easier for him.