String.compareTo might or might not be what you need.
Take a look at this link if you need localized ordering of strings.
Answer from Buhb on Stack Overflowjava - Comparing strings by their alphabetical order - Stack Overflow
[Intro Comp. Sci.] Sorting an ArrayList of Strings Alphabetically in Java?
Collections.sort uses the natural ordering of objects in the ArrayList, which for String objects is Lexicographically. Collections.sort( ArrayList < String > ) should work the way you want.
More on reddit.comTips for printing two Strings in alphabetical order?
How to compare characters in two strings to sort them alphabetically? (without c string library functions)
Videos
String.compareTo might or might not be what you need.
Take a look at this link if you need localized ordering of strings.
Take a look at the String.compareTo method.
Copys1.compareTo(s2)
From the javadocs:
The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
I'm working with an ArrayList of Animal objects, each with a String name that can be entered and added. I want them ordered alphabetically, but I'm not sure how to do this.
I know to override the CompareTo method after implementing comparable and to use the Collections.sort method, but then what, and how?
My idea was to somehow implement an ArrayList of characters and use charAt to find the compare two Strings with each other, but I can't really figure out if this is the best way, or if there is a better way.
Surely, I cannot be the first person to alphabetize a list of Strings in an array, so how is this typically done? Thanks!
I should add, I know to use CompareTo and Collections already. What I don't know is which algorithm to implement in CompareTo that will actually compare two Strings alphabetically.
I'm taking a CS 1 course and we're using Zybooks for some java homework, and I'm stuck on one of the problems. The problem states:
Print the two strings in alphabetical order. Assume the strings are lowercase. End with newline. Sample output:
capes rabbits
I have this so far, and the first test is right, but the second one is wrong. I want to be able to successfully complete all of the "tests"
My code so far:
import java.util.Scanner;
public class OrderStrings {
public static void main (String [] args) {
String firstString;
String secondString;
firstString = "rabbits";
secondString = "capes";
if(secondString != firstString) {
System.out.println(secondString+" "+firstString);
}
else{
System.out.println(firstString+" "+secondString);
}
}
}The first test outputs this
capes rabbits
the output that is wrong displays this for some reason:
your input: rabbits capes
expected output: capes rabbits
why does the second output switch around the Strings?
Everything before the if statement was already made for me by Zybooks.