I need to compare the following characters c,d,h,s

such that

s will be the biggest
followed by h , c
and d the smallest.

d < c < h < s

I have no idea how should I implement it.
please help.

Recommended Answers

All 4 Replies

create an enum, which has the value of a char as value. add the nr (of order) as a field field, use this to compare them

chars in Java are numeric types - they are 16 bit integers that are used to represent characters in unicode. So its pefectly OK to compare them with > or < operators. Unicode 'a' - 'z' are represented by the numeric values 97-122 respectively, so the comparisons follow normal alphabetic order, ie 'c' < 'd' < 'h' < 's'
Are you sure you want to compare 'd' < 'c'? If so, what about 'a', 'b' and 'e'?

Ahha! Just seen the significance of those 4 letters. stultuske had the answer: they should be an enum, although there's no need to add the number because enums have a ordinal() method that returns their position in the order in which they were declared. (And yes, you'll want another enum with 13 values.)

thanks I will try it with enums

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.