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

Thread: how to sort arraylist of strings?

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default how to sort arraylist of strings?

    hi I have an Array List of <strings> holding phone numbers and index numbers separated by tab
    example:

    45 0876565645
    14 0876476453
    09 0876575657
    111 0876575657
    8211 0876575657

    my question is how would I sort this list in order of first group of numbers while ignoring the phone numbers?
    example:
    09...
    14...
    45...
    111...
    8211...

    any ideas would be great as all I can think of is a load of if statements but this list is 100+ long so that probably wont work


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: how to sort arraylist of strings?

    Implement a Comparator.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: how to sort arraylist of strings?

    Quote Originally Posted by PhHein View Post
    Implement a Comparator.
    sorry i am a noob but i did look at the comparator class but how would it work without taking into account the phone numbers ?
    i only want to sort them based on the index numbber

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: how to sort arraylist of strings?

    Split away the telephone number in the Comparator and just compare the indexes.

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: how to sort arraylist of strings?

    Quote Originally Posted by PhHein View Post
    Split away the telephone number in the Comparator and just compare the indexes.

    how ?? didint think that could be done without splitting the list into 2 lists and then joining them together which would just be a big mess is there an inbuilth function for this by the way this is code i am testing on
     ArrayList<String>a=new ArrayList<String>();
            a.add("123    2345462335:454");
            a.add("23    133445645335:454");
            a.add("1783    23456435:454");
            Collections.sort(a);
            for(String h:a){
                System.out.println(h);
            }
    which ouptus
    123    2345462335:454
    1783    23456435:454
    23    133445645335:454

    thanks for help

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: how to sort arraylist of strings?

    No, you've got the string with the full number, and strings are immutable. So you extract the index into a new String and compare that to another extracted index.

  7. #7
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: how to sort arraylist of strings?

    so i am going to have create a bunch of lists and then join em back together DAMB!

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: how to sort arraylist of strings?

    NOOOO! You leave the list as is. Just extract the index in your compare method.
    public int compare(String no1, String no2) {
    		String index1 = getindex(no1);
    		String index2 = getindex(no2);
    		return index1.compareTo(index2);
    	}
     
    	private String getindex(String no1) {
    		// TODO Auto-generated method stub
    		return null;
    	}

Similar Threads

  1. Reverse the order of Strings in an ArrayList?
    By jean28 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 8th, 2012, 11:25 PM
  2. Strings not being added to arraylist
    By noel222 in forum What's Wrong With My Code?
    Replies: 30
    Last Post: February 10th, 2012, 05:53 PM
  3. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 03:27 PM
  4. How to sort an ArrayList of Strings?
    By noFear in forum Java Theory & Questions
    Replies: 6
    Last Post: September 15th, 2010, 03:23 PM
  5. 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