Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ArrayList Sorting Problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation ArrayList Sorting Problem

    Okay, we are just learning how to read in from a file and I'm having trouble with what seems like the easiest part. I need to sort my ArrayList full of objects which are "Shapes" by name first, and then by the size of their area. The name one is giving me the most trouble now because I'm not really sure how to compare strings. The Shape's names are as follows shape1,shape2,shape3,shape4,.....shape252. They are read in as are on the txt file (in a random order) and now need to be sorted by name. I have methods which can return a string of said shape's name , but how do I go about comparing "shape15" vs "shape5" obviously shape 5 should go first in this situation.

    I would like to avoid the many "OH DIS EASY JUST IMPLEMENT COMPAREABLE BLAH BLAH" can't do that for this assignment it basically has to be our own algorithm.

    as far as area goes I think I'll be able to just compare the returned values of the shape's area and just sort them in that order. numbers always seem to make sense, so I just need a little help with the String.
    Last edited by coke32; April 24th, 2012 at 08:55 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Sorting Problem

    ... I absolutely cannot ... figure out how to sort this ArrayList.. please I'm literally begging.. this is the ONLY thing i can't do its been 4 hours now of searching online/reading and i still can't figure out how to sort an ArrayList.... I've come to the conclusion that when getting the names I'm going to be using the String.compareTo(string) method but HOW for the love of GOD do I sort it then.. so ArrayList.get(i).getName.CompareTo(ArrayList.get(z ).getName) this returns a number... okay now what..

    editToMod: Sorry got worked up, I understand.
    Last edited by coke32; April 24th, 2012 at 08:56 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayList Sorting Problem

    Please watch your language (your post has been edited)...and there are dozens if not hundreds of posts here - mentioning a sense of urgency (eg it is due at midnight) can be considered rude to all those others with similar problems, who have the same urgency as you.

    To the question at hand...see the Collections class sort methods
    Collections (Java Platform SE 6)
    or write your own:
    Sorting algorithm - Wikipedia, the free encyclopedia
    Last edited by copeg; April 24th, 2012 at 08:56 PM.

  4. #4
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Sorting Problem

    I am thinking that if the name format for the shapes is String followed by number, you need to write code that step by step works through the name looking for the int values and then once isolated can simply sort the shapes using those numbers.

    I would suggest starting from the end of the name that has the int values and stopping when you get to a string.

    Good luck.

Similar Threads

  1. Problem with sorting by alphabetical order
    By pikapika in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 6th, 2012, 09:58 PM
  2. Sorting problem
    By Kaisshau in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 28th, 2011, 10:05 PM
  3. Sorting ArrayList
    By leao in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2011, 03:33 PM
  4. Sorting a Arraylist
    By waterjames in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 02:47 PM
  5. Problem with ArrayList
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2010, 08:37 PM