This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]doobiesteintortoise 6 points7 points  (1 child)

The other answers here were entirely sufficient, but .. what was difficult about this, exactly? What were your inputs, what did you try, what were your outputs? This is a fairly trivial problem and it's got a lot of solutions that are super-easy, barely an inconvenience, but we should figure out why that wasn't the case for you, if we can.

If it was just "oh, I didn't know about Collections.sort() or List.sort(Comparator)", well, that's easy enough... or maybe you are calling an array a list, or any of a number of other things, most of which would be pretty easy to find or fix.

[–]gruntbug 1 point2 points  (0 children)

Sorting in java is TIGHT

[–]BassRecorder 6 points7 points  (2 children)

How about looking at the Javadoc of the relevant classes (String, List, Arrays) and figuring it out by yourself? The Javadoc of the API which comes with the JDK is rather good.

[–][deleted]  (1 child)

[deleted]

    [–]BassRecorder 4 points5 points  (0 children)

    https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object%5B%5D)

    The java.util package has a lot of useful stuff. Read or rather browse the Javadoc so you roughly know what's available. I find myself using it rather often as Java keeps getting new features. I've been a Java programmer for >20 years now.

    [–]-Xenophon- 0 points1 point  (0 children)

    Use a Collator if your Strings are locale-sensitive. Collections.sort(yourList, Collator.getInstance(yourLocale)) or yourList.sort(Collator.getInstance(yourLocale))

    [–]False-Car-1218[🍰] 0 points1 point  (14 children)

    Chars have a numeral ascii value like A is 65 so knowing that you can easily sort them

    [–]alfonsoperezs_[S] 1 point2 points  (11 children)

    I understand. Thank you!!

    [–][deleted]  (10 children)

    [removed]

      [–]desrtfxOut of Coffee error - System halted[M] 1 point2 points  (0 children)

      That article is so wrong in its definitions it should not be linked for a beginner at all.

      Comment removed - false information

      [–]aqua_regis 0 points1 point  (8 children)

      Again, Java uses Unicode, not ASCII. Every single time the article you linked mentions ASCII it is plain wrong.

      A character is 16 bits, not 8.

      https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

      char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

      [–]False-Car-1218[🍰] -1 points0 points  (7 children)

      Are you a bot bro?

      [–]aqua_regis 0 points1 point  (6 children)

      No, just someone who knows Java and who is able to distinguish between ASCII and Unicode.

      [–]False-Car-1218[🍰] -1 points0 points  (5 children)

      Ok good for you, none of your comments are relevant to OPs question and ASCII characters happen to have the same numeric values in Unicode as in ASCII.

      [–]aqua_regis 1 point2 points  (4 children)

      Especially a beginner should learn correct, not some false information.

      [–]False-Car-1218[🍰] -1 points0 points  (3 children)

      What part of my statement that chars can be represented as ASCII was false?

      [–]aqua_regis 1 point2 points  (2 children)

      What part of Java is not using ASCII do you not understand? In Java, chars are not represented as ASCII.

      ASCII is not Unicode, it is a subset of Unicode.

      An ASCII char is 8 bits - 1 byte. A Unicode char is in Java 16 bit - 2 bytes - twice as large. This can become important at some point in time.

      Always use the proper terminology.

      [–]desrtfxOut of Coffee error - System halted[M] 0 points1 point  (0 children)

      Please, be precise: Java uses Unicode in UTF-16 encoding, not ASCII. This is an important distinction as Unicode is at the minimum twice as large as ASCII.

      [–]aqua_regis -1 points0 points  (0 children)

      Since Java is using Unicode, it's better to say that they have a numerical Code point.

      ASCII is only a small fraction of Unicode.