Results 1 to 5 of 5
Thread: Sorting arrays
- 08-08-2011, 01:06 PM #1Member
- Join Date
- Jul 2011
- Rep Power
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
- 08-08-2011, 01:32 PM #2
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.
- 08-08-2011, 03:12 PM #3
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.
- 08-09-2011, 01:28 AM #4
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.If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-09-2011, 02:19 AM #5
- By anfielder in forum New To JavaReplies: 5Last Post: 12-14-2010, 07:12 AM
- By woser in forum New To JavaReplies: 0Last Post: 11-06-2010, 11:21 AM
- By Joycey in forum New To JavaReplies: 4Last Post: 03-30-2010, 08:35 PM
- By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM
- By Faye Rett in forum New To JavaReplies: 4Last Post: 03-07-2010, 01:00 AM