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 7 of 7

Thread: How to sort an ArrayList of Strings?

  1. #1
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to sort an ArrayList of Strings?

    I want to make different type of sort. I have an ArrayList of Strings. When the information is shown I want all the Strings beginning with "blablabla" to be shown first. How can I achieve that?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to sort an ArrayList of Strings?

    Look at the Collections class. It has sort methods you can use. You'll need your own code to determine the sort order.

  3. #3
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: How to sort an ArrayList of Strings?

    Try to look at Comparator and Comparable interfaces. You have to implement one of these or both interfaces, implement some methods and then sort the method by calling Collections.sort() or something. I can't tell exactly how it goes because I haven't done that for a long time, but the idea should be clear to you. Try to look for some examples with these interfaces.
    Last edited by Asido; September 15th, 2010 at 04:33 AM.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How to sort an ArrayList of Strings?

    Quote Originally Posted by Asido View Post
    Try to look at Comparator and Comparable interfaces. You have to implement one of these or both interfaces
    String already implements Comparable.

    db

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to sort an ArrayList of Strings?

    Strings beginning with "blablabla" to be shown first
    That will require the OP to use his own code to set the order.

  6. #6
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: How to sort an ArrayList of Strings?

    Quote Originally Posted by Darryl.Burke View Post
    String already implements Comparable.

    db
    In this case just override the method compareTo()

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

    Default Re: How to sort an ArrayList of Strings?

    Quote Originally Posted by Asido View Post
    In this case just override the method compareTo()
    String is final cannot be extended, so its methods cannot be overridden. The Comparable interface must be implemented in some other way.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  3. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM