Results 1 to 5 of 5

Thread: Sorting arrays

  1. #1
    Lex
    Lex is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    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

  4. #4
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Sorting Two Dimensional arrays
    By anfielder in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 07:12 AM
  2. Arrays and sorting function
    By woser in forum New To Java
    Replies: 0
    Last Post: 11-06-2010, 11:21 AM
  3. Help with sorting arrays
    By Joycey in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 08:35 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 01:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •