Comparing Strings by Their Alphabetical Order

Comparing strings by their alphabetical order

String.compareTo might or might not be what you need.

Take a look at this link if you need localized ordering of strings.

Compare two strings by order in alphabet in java

Since String implements Comparable you can use compareTo to compare chronologically:

string1.compareTo(string2)

Comparing two string and sorting them in alphabetical order

Hint: All basic data type classes in java implement Comparable interface.

String a="LetterA";
String b="ALetterB";
int compare = a.compareTo(b);
if (compare < 0){
System.out.println(a+" is before "+b);
}
else if (compare > 0) {
System.out.println(b+" is before "+a);
}
else {
System.out.println(b+" is same as "+a);
}

Can't compare strings by alphabetical order

It seems you actually want to sort these strings by their ordinal value (their binary representation). In that case use StringComparison.Ordinal as comparisonType.

 string.Compare(third, fourth, StringComparison.Ordinal)); //output: -27
string.Compare(second, third, StringComparison.Ordinal)); //output: -1
string.Compare(first, second, StringComparison.Ordinal)); //output: -1

Weirdly the first comparison yields -27 instead of -1. The Compare method only specifies that the return value will be <1, 0 or >1 so those three output are essentially (and semantically) the same result.

comparing 2 strings alphabetically for sorting purposes

Lets look at some test cases - try running the following expressions in your JS console:

"a" < "b"

"aa" < "ab"

"aaa" < "aab"

All return true.

JavaScript compares strings character by character and "a" comes before "b" in the alphabet - hence less than.

In your case it works like so -

1 . "a​aaa" < "​a​b"

compares the first two "a" characters - all equal, lets move to the next character.

2 . "a​a​​aa" < "a​b​​"

compares the second characters "a" against "b" - whoop! "a" comes before "b". Returns true.

Compare strings based on alphabetical ordering

this should work:

if len(x)==len(y):
return min(x,y)

Is string::compare reliable to determine alphabetical order?

According to the docs at cplusplus.com,

The member function returns 0 if all
the characters in the compared
contents compare equal, a negative
value if the first character that does
not match compares to less in the
object than in the comparing string,
and a positive value in the opposite
case.

So it will sort strings in ASCII order, which will be alphabetical for English strings (with no diacritical marks or other extended characters) of the same case.

Comparing 2 strings based on their alphabetical order and frequency

The requirement is, if 2 words have the same frequency, then we should sort in ascending fashion, but if the two words do not have the same frequency, then the word with largest frequency comes first in priority queue below.

There are two cases.

  1. Frequency of two strings are not same. Here higher freq should come first means descending sort.
  2. Frequency of two strings are same. Here ascending sort should be applied.

1st Case

return freq1 - freq2;

It ensures ascending sort. But later it is reversed to ensure descending sort. It is for the first case.

2nd Case

word2.compareTo(word1)

It ensures descending sort (second compared with first). Later reversed to ensure ascending sort. It is the second case.

Simply put, as for 2 cases sorting is different that's why for 1 case, freq1-freq2 is used and for another one word2.compareTo(word1) is used. If both had same sorting then we would have used, freq1-freq2 and word1.compareTo(word2).

Comparing strings for alphabetical order in Bash, test vs. double bracket syntax

I have come up with my own solution to the problem, however I must first thank @GordonDavisson and @LéaGris for their help and for what I have learned from them as that is invaluable to me.

No matter if computer or human locale is used, if, in an alphabetical sort, apple comes after Apple, then it also comes after Banana and if Banana comes after apple, then Apple comes after apple. So I have come up with the following:

# A function which sorts two words alphabetically with lower case coming after upper case.
# The last word in the sort will be printed twice to demonstrate that this works for both
# the POSIX compliant single bracket test call and the newer double bracket condition
# syntax.
# arg 1: One of two words to sort
# arg 2: One of two words to sort
# Return: 0 upon completion, 1 if incorrect number of args is given
sort_alphabetically() {
[ $# -ne 2 ] && return 1

word_1_val=0
word_2_val=0

while read -n1 letter; do
(( word_1_val += $(printf '%d' "'$letter") ))
done < <(echo -n "$1")

while read -n1 letter; do
(( word_2_val += $(printf '%d' "'$letter") ))
done < <(echo -n "$2")

if [ $word_1_val -gt $word_2_val ]; then
echo $1
else
echo $2
fi

if [[ $word_1_val -gt $word_2_val ]]; then
echo $1
else
echo $2
fi

return 0
}

sort_alphabetically "apple" "Apple"
sort_alphabetically "Banana" "apple"
sort_alphabetically "aPPle" "applE"

prints:

apple
apple
Banana
Banana
applE
applE

This works using process substitution and redirecting the output into the while loop to read one character at a time and then using printf to get the decimal ASCII value of each character. It is like creating a temporary file from the string which will be automatically destroyed and then reading it one character at a time. The -n for echo means the \n character, if there is one from user input or something, will be ignored.

From bash man pages:

Process Substitution

Process substitution allows a process's input or output to be referred to using a filename. It takes the form of <(list) or >(list). The process list is run asynchronously, and its input or output appears as a filename. This filename is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list. Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.

from stackoverflow post about printf:

If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.

Note: process substitution is not POSIX compliant, but it is supported by Bash in the way stated in the bash man page.


UPDATE: The above does not work in all cases!


The above solution works in many cases however we get some anomalies.



Leave a reply



Submit