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

Thread: How to sort this?

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default How to sort this?

    In my program I have put these in a arrayList:

    String string1 = start + "-" + end;

    So arrayList is full of these string1 things.
    Now start and end are both number.

    Is there a way that i can sort the arrayList on the start?
    Like that I want 1-4 before 3-2?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to sort this?

    7 - 7
    2 - 3
    7 - 5
    4 - 3
    4 - 2
    6 - 9
    4 - 6
    9 - 4
    3 - 5
    9 - 8
    5 - 4
    3 - 3

    Sort the set above on paper. Think about the steps taken to do so, and write them down. Know how to solve the problem before trying to solve the problem in code.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How to sort this?

    Quote Originally Posted by Purple01 View Post
    ...So arrayList is full of these string1 things.
    Now start and end are both number.

    Is there a way that i can sort the arrayList on the start?
    Like that I want 1-4 before 3-2?
    One way would be to create a custom comparator and use Collections.sort. It could go like this:
        class CustomComparator implements Comparator<String>
        {
            @Override
            public int compare(String str1, String str2)
            {
                  //Code that extracts the integer values of substrings of str1 and str2
                  // Call them int1 and int2
                  // Return int1-int2
            }

    Then, call the Collections.sort function with your ArrayList<String> object like this:

            ArrayList<String> al = new ArrayList();
     
          //
          // Code to populate al goes here
          //
     
            System.out.println("Original: ");
            for (String str : al) {
                System.out.println("  " + str);
            }
     
            // For amusement, just sort by comparing strings
            Collections.sort(al);
            System.out.println("Natural sort of the Strings: ");
            for (String str : al) {
                System.out.println("  " + str);
            }
     
            // The real goal: Sort by numeric value of the first item in the "num1-num2" Strings
            Collections.sort(al, new CustomComparator());
            System.out.println("CustomComparator sort of the Strings: ");
            for (String str : al) {
                System.out.println("  " + str);
            }

    Example output
    Original: 
      1-10
      21-18
      25-4
      25-12
      1-9
      17-12
      10-16
      2-4
      17-13
      2-15
    Natural sort of the Strings: 
      1-10
      1-9
      10-16
      17-12
      17-13
      2-15
      2-4
      21-18
      25-12
      25-4
    CustomComparator sort of the Strings: 
      1-10
      1-9
      2-15
      2-4
      10-16
      17-12
      17-13
      21-18
      25-12
      25-4



    Cheers!

    Z
    Last edited by Zaphod_b; October 11th, 2012 at 10:51 AM.

Similar Threads

  1. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  2. 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
  3. Timing a sort
    By joshft91 in forum Collections and Generics
    Replies: 1
    Last Post: February 7th, 2011, 05:12 PM
  4. Insertion Sort
    By Kimimaru in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2010, 06:26 AM
  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