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

Thread: Sorting lowercase strings before uppercase strings

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Sorting lowercase strings before uppercase strings

    I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sorting lowercase strings before uppercase strings

    Sure. Just use a custom Comparator.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting lowercase strings before uppercase strings

    	public int compare(String arg0, String arg1) {
    		for(int i=0;i<arg0.length() && i<arg1.length();i++) {
    			if((arg0.charAt(i) > 64 && arg0.charAt(i) < 91) || (arg0.charAt(i) > 97 && arg0.charAt(i) < 122)) {
    				if(arg0.charAt(i) < arg1.charAt(i)) return 1;
    				if(arg0.charAt(i) > arg1.charAt(i)) return -1;
    			} else {
    				if(arg0.charAt(i) < arg1.charAt(i)) return -1;
    				if(arg0.charAt(i) > arg1.charAt(i)) return 1;
    			}
    		}
    		return 0;
    	}


    --- Update ---

    Here is a sample usage:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    public class Sorter implements Comparator<String>
    {
    	@Override
    	public int compare(String arg0, String arg1) {
    		for(int i=0;i<arg0.length() && i<arg1.length();i++) {
    			if((arg0.charAt(i) > 64 && arg0.charAt(i) < 91) || (arg0.charAt(i) > 97 && arg0.charAt(i) < 122)) {
    				if(arg0.charAt(i) < arg1.charAt(i)) return 1;
    				if(arg0.charAt(i) > arg1.charAt(i)) return -1;
    			} else {
    				if(arg0.charAt(i) < arg1.charAt(i)) return -1;
    				if(arg0.charAt(i) > arg1.charAt(i)) return 1;
    			}
    		}
    		return 0;
    	}
    	public static void main(String[] a) {
    		List<String> l = new ArrayList<String>();
    		l.add("String1");
    		l.add("STRING1");
    		l.add("STRING2");
    		l.add("String2");
    		/*Normal sort*/
    		Collections.sort(l);
    		System.out.println(l.toString());
    		/*Custom sort*/
    		Collections.sort(l, new Sorter());
    		System.out.println(l.toString());
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Sorting lowercase strings before uppercase strings

    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    KevinWorkman (March 27th, 2014)

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting lowercase strings before uppercase strings

    Thanks Norm. I agree with the rule.

Similar Threads

  1. Replies: 4
    Last Post: November 28th, 2012, 03:01 PM
  2. [SOLVED] Sorting Strings
    By userct in forum Algorithms & Recursion
    Replies: 17
    Last Post: February 28th, 2012, 10:03 AM
  3. [SOLVED] Sorting strings alphabetically
    By userct in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 21st, 2012, 04:38 PM
  4. Uppercase and Lowercase in a string
    By sselasky1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2012, 03:01 PM
  5. [SOLVED] Method of Sorting? by assiging numerical values to Strings
    By Mirage in forum Java Theory & Questions
    Replies: 0
    Last Post: June 16th, 2010, 07:49 PM